Event listeners does not trigger in expo, react-navigation but use to work previously - expo

For :
expo --version 4.9.0 and
"#react-navigation/bottom-tabs": "^5.11.11",
"#react-navigation/drawer": "^5.12.5",
"#react-navigation/native": "^5.9.4",
"#react-navigation/stack": "^5.14.5",
Event listeners never get triggered after an event occurrences.
useFocusEffect(
React.useCallback(() => {
// Clearing all the pending information to be transferred..
OperationPending.emit("notPending");
// Listening on the refresh event
RefreshEventEmitter.addListener("refresh", () => {
sendMqttCmd("getTime");
});
// Listening for connection status on this event...
MqttConnection.addListener("status", () => {
// Setting the connection status
setIsConnected(getWSConnStatus());
});
let response = setTimeout(() => setNoResponse(true), 20000);
console.log("Just before event..");
// Creating a new listeners to execute on this event..
MqttEvents.addListener("freshDataAvailable", () => {
console.log("Executed event in getDevice");
// ----------------------------------------------
// Try to parse the got mqtt data object and if found error, handle
// it in catch section..
try {
// If length of the array to be rendered by flatlist is not 0
// then page is not refreshing any more!! Show the data..
if (getDeviceTime().hr !== undefined) {
console.log("Executed in getDevic");
// If time is readdy to fetched..
let mqttTime = getDeviceTime();
setDateTimeObj({
hourCount: Number(mqttTime.hr),
minCount: Number(mqttTime.min),
secCount: Number(mqttTime.sec),
});
// Also clear the timeout checking for no response as we got response
clearTimeout(response);
// Setting isPageRefreshing to false, let flatlist render the items
setTimeout(() => setIsPageRefeshing(false), 500);
}
} catch (error) {}
// Increasing the count, re-renders the screen, what we exactly want..
// setCount(count + 1);
});
// ----------------------------------------------
// Removing all the listeners, so that screen does not re-render after unmounting..
const unsubscribe = () => {
console.log("Also unsubscribed..");
clearTimeout(response);
clearInterval(shouldRenderInterval);
MqttConnection.removeAllListeners();
RefreshEventEmitter.removeAllListeners();
MqttEvents.removeAllListeners();
};
// ----------------------------------------------
// Returned function is called when screen is unmounted from display!!
return () => unsubscribe();
}, [
count,
MqttEvents,
OperationPending,
RefreshEventEmitter,
secCount,
isPageRefreshing,
])
);
On event triggering no console log is shown!!
Hopefully I'll get solution here,
Regards..

Related

jest.spyOn mock return value not returning value

The code I'm trying to test:
const utils = require('../utils/utils');
let imageBuffer;
try {
imageBuffer = await utils.retrieveImageFromURI(params)
console.log(imageBuffer) // comes back as undefined when I mock the utils.retreieveImageFromURI
if (!imageBuffer || imageBuffer.length < 1024) {
throw new Error(`Retrieve from uri (${params.camera.ingest.uri}) was less than 1kb in size - indicating an error`)
}
console.log(`${params.camera.camId} - Successful Ingestion from URI`);
} catch (err) {
reject({ 'Task': `Attempting to pull image from camera (${params.camera.camId}) at ${params.camera.ingest.uri}`, 'Error': err.message, 'Stack': err.stack })
return;
}
Specifically, I'm trying to mock the utils.retrieveImageFromURI function - which has API calls and other things in it.
When I try to mock the function using spyOn I am trying it like so:
describe("FUNCTION: ingestAndSave", () => {
let fakeImageBuffer = Array(1200).fill('a').join('b'); // just get a long string
console.log(fakeImageBuffer.length) //2399
let retrieveImageFromURISpy
beforeAll(() => {
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(fakeImageBuffer)
})
test("Will call retrieveImageFromURI", async () => {
await ingest.ingestAndSave({camera:TEST_CONSTANTS.validCameraObject, sourceQueueURL:"httpexamplecom", receiptHandle: "1234abcd"})
expect(retrieveImageFromURISpy).toHaveBeenCalledTimes(1)
})
afterEach(() => {
jest.resetAllMocks()
})
afterAll(() => {
jest.restoreAllMocks()
})
})
When I do this, I get a console log that imageBuffer (which is supposed to be the return of the mocked function) is undefined and that, in turn, triggers the thrown Error that "Retrieve from uri ...." ... which causes my test to fail. I know I could wrap the test call in a try/catch but the very next test will be a "does not throw error" test... so this needs to be solved.
It's not clear to me why the mockReturnValue isn't getting returned.
Other steps:
I've gone to the REAL retrieveImageFromURI function and added a console log - it is not running.
I've changed mockReturnValue to mockImplementation like so:
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
console.log("Here")
return fakeImageBuffer
})
And it does NOT console log 'here'. I'm unsure why not.
I have also tried to return it as a resolved Promise, like so:
retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockImplementation(() => {
console.log("Here")
return Promise.resolve(fakeImageBuffer)
})
Note, this also doesn't console log.
I've also tried to return the promise directly with a mockReturnValue:
`retrieveImageFromURISpy = jest.spyOn(utils, 'retrieveImageFromURI').mockReturnValue(Promise.resolve(fakeImageBuffer)`)

Checkpointing Performance Issue on azure event hubs using kafka.js

We are using kafka.js event client with event hubs and we are seeing performance issues with kafka.js. The event client was originally running on Azure SDK event client for event hubs and we recently upgraded to kafka.js.
The issue is it's taking longer time to checkpoint before committing the event.
Azure SDK used to take 15ms to checkpoint (using external Azure storage account outside of event hubs) vs new kafka.js event client relying on Azure event hubs internal storage for checkpointing but taking more than 1 second to checkpoint. Extremely slow.
We are using eachMessage and checkpointing after each message to ensure data consistency but Azure event hubs throttles 4 calls/second per partition and each checkpoint call takes close to 1 second due to shared internal checkpoint storage used by event hubs.
We tried using eachBatch with autoCommit but it seems to be committing entire batch of messages in case if it fails during batch processing.
Anyone noticed similar issue elsewhere? appreciate any feedback or suggestion to solve this.
Here is the code snippet:
/**
* #method consume()
*/
public async consume() {
await this.client.run({
eachBatchAutoResolve: this.configuration.eachBatchAutoResolve, // default to false so consumer does not auto-resolve the offsets
autoCommitThreshold: this.configuration.autoCommitThreshold,
partitionsConsumedConcurrently: this.configuration.partitionsConsumedConcurrently,
eachBatch: async ({ batch, resolveOffset, heartbeat }) => {
if (this.shutdownContext != null) {
// prevent processing new events during a shutdown.
return;
}
const { topic, partition } = batch;
const partitionId = `${topic}-${partition}`;
if (this.partitionTracker.isPartitionProcessing(partitionId)) {
// prevent concurrent event processing on the same partition.
return;
}
this.partitionTracker.startProcessing(partitionId);
this.genericLogger.debug(
`batch last offset: ${batch.lastOffset()} partitionId: ${partitionId} highWatermark: ${
batch.highWatermark
} offsetLag: ${batch.offsetLag()}`,
);
/* eslint-disable no-await-in-loop, no-restricted-syntax */
for (const message of batch.messages) {
let event: Event<any>;
const partitionKey = message.key ? message.key.toString() : message.key;
const { offset, timestamp } = message;
// build event context
const context: EventContext = {
topic,
partition,
partitionKey,
offset,
timestamp,
};
try {
// transform kafka message to event
event = await this.transform(message);
} catch (error) {
const id = uuid.v4();
this.genericLogger.crit(
`Event failed consumer transformation. Please check the Dead Letter topic for a version of the event:${id}`,
);
const poisonousEvent: Event<any> = {
id,
correlationId: { id, origin: uuid.v4() },
data: message.value,
type: 'ca.event.poisonousmessage.created.v1',
};
await this.dlqProducer.send(
poisonousEvent,
this.configuration.dlqTopic,
this.genericLogger,
);
try {
resolveOffset(message.offset);
await heartbeat();
// await commitOffsetsIfNecessary();
this.partitionTracker.stopProcessing(partitionId);
await this.shutdownCheck(context);
} catch (commitError) {
await this.signalShutdown(
ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
commitError,
);
await this.shutdown({
reason: ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
error: commitError,
eventContext: context,
});
}
break;
}
const contextLogger = getLoggerFactory(
this.configuration.loggerOptions,
'event-client-consumer',
)(getEventMetaData(event));
const eventMessageContext = `[eventHubName:${topic} partition:${partition} offset:${offset} partitionKey:${partitionKey} timestampUTC:${timestamp}]`;
contextLogger.debug(`received new message on ${eventMessageContext}.`);
try {
// heartbeat
await heartbeat();
} catch (error) {
error.message = `error while manually sending a heartbeat: ${error.message}`;
if (error.message.includes(REBALANCING)) {
contextLogger.warn(error.message);
} else {
contextLogger.error(error.message);
}
break;
}
try {
// process
await this.process(event, context, contextLogger, heartbeat);
resolveOffset(message.offset);
// commit
// await this.commit(
// event,
// context,
// contextLogger,
// message.offset,
// resolveOffset,
// commitOffsetsIfNecessary,
// );
} catch (error) {
if (error.message.includes(REBALANCING)) {
contextLogger.warn(
`rebalancing error during processing or committing: ${error.message}`,
);
throw error;
}
const exception = error as Exception;
switch (exception.type) {
case ProcessExceptionType.COMMIT: {
contextLogger.error(
`[Event Processing Error] failed to commit processed event on ${eventMessageContext}. error: ${error.message}.`,
error,
);
// failed to commit message after retry. sleeping.
await this.sleep(context, error as Error, exception.type, heartbeat);
// await this.signalShutdown(exception.type, error);
// await this.shutdown({ reason: exception.type, error, eventContext: context });
throw error;
}
case ProcessExceptionType.SHUTDOWN_NO_COMMIT: {
// shutdown with no commit. most likely due to non retryable circuit breaker exception.
await this.sleep(context, error as Error, exception.type, heartbeat);
// await this.signalShutdown(exception.type, error);
// await this.shutdown({ reason: exception.type, error, eventContext: context });
throw error;
}
case ProcessExceptionType.SLEEP_NO_COMMIT: {
// return early for sleep cycle.
throw error;
}
default: {
if (this.configuration.continueOnFailedEventProcessing) {
contextLogger.crit(
`[Event Processing Error] Error in user defined code when processing event on ${eventMessageContext} beyond process loop retry limit error: ${error.message}. Commiting event to continue processing new events.`,
error,
);
try {
resolveOffset(message.offset);
await heartbeat();
// await commitOffsetsIfNecessary();
this.partitionTracker.stopProcessing(partitionId);
await this.shutdownCheck(context);
} catch (commitError) {
await this.sleep(context, commitError as Error, exception.type, heartbeat);
// await this.signalShutdown(
// ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
// commitError,
// );
// await this.shutdown({
// reason: ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
// error: commitError,
// eventContext: context,
// });
}
} else {
contextLogger.crit(
`[Event Processing Error] Error in user defined code when processing event on ${eventMessageContext} beyond process loop retry limit error: ${error.message}. Initiating sleep.`,
error,
);
await this.sleep(context, error as Error, exception.type, heartbeat);
// await this.signalShutdown(
// ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
// error,
// );
// await this.shutdown({
// reason: ProcessExceptionType.FAILURE_TO_PROCESS_AND_COMMIT,
// error,
// eventContext: context,
// });
}
}
}
throw error;
}
}
this.genericLogger.debug(
`batch last offset: ${batch.lastOffset()} partitionId: ${partitionId} highWatermark: ${
batch.highWatermark
} offsetLag: ${batch.offsetLag()}`,
);
this.genericLogger.debug(
`resolved batch of ${batch.messages.length} messages partitionId: ${partitionId}`,
);
this.partitionTracker.stopProcessing(partitionId);
await this.shutdownCheck();
},
});
}

vue.js test:unit w test-utils & Jest : [vue-test-utils]: wrapper.destroy() can only be called on a Vue instance

In order to test a beforeDestroy() hook in my component, I wrote the following spec :
it("should test lifecycle when audio tag is destroyed", () => {
// jsdom doesn't support any loading or playback media operations.
// As a workaround you can add a few stubs in your test setup:
window.HTMLMediaElement.prototype.removeEventListener = () => { /* do nothing */ };
// given
const wrapper = mount(AudioPlayer, {
// attachToDocument: true,
propsData: {
autoPlay: false,
file: file,
ended,
canPlay
}
});
wrapper.vm.loaded = true; // enable buttons
const player = wrapper.find("#player");
expect(wrapper.contains('#playPauseBtn')).toBe(true);
// when
player.destroy()
// then
expect(wrapper.contains('#playPauseBtn')).toBe(false);
});
but I am getting an error, even if the destroy() is used as in the doc ...
[vue-test-utils]: wrapper.destroy() can only be called on a Vue instance
177 | expect(wrapper.contains('#playPauseBtn')).toBe(true); // OK
178 | // when
> 179 | player.destroy()
where am I wrong ?
thanks for feedback
const player = wrapper.find("#player"); returns a wrapper of DOM element, so basically HTML.
destroy() destroys a Vue component instance.
You cannot call destroy function on an "HTML element". I believe you wanted to write wrapper.destroy()

angular2: how to test a component which has an observable time interval

I have a slide-show component that has an Input array of slide objects and shows each one as long as it's been defined in slide.time of itself. also there are two buttons that clicking them has to slide to the next item and reset the timer. in order to make this work, I'm using Observables like this:
/**
* a "SUBJECT" for pausing(restarting) the slider's auto-slide on user's click on left and right arrows
* #type {Subject}
*/
private pauser = new Subject();
/**
* the main observable for timer (before adding the pause/reset option)
* #type {Observable<T>}
*/
private source = Observable
.interval(1000)
.timeInterval()
.map(function (x) { /*return x.value + ':' + x.interval;*/ return x })
.share();
/**
* the final timer, which can be paused
* #type {Observable<R>}
*/
private pausableSource = this.pauser.switchMap(paused => paused ? Observable.never() : this.source);
/**
* the subscription to the timer which is assigned at OnInit hook , and destroyed at OnDestroy
*/
private subscription;
ngOnInit(){
this.subscription = this.pausableSource.subscribe(() => {
//doing changes to the template and changing between slides
});
this.pauser.next(false);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
and it's working properly.
now to test this component, I'm giving it some data within a test-host component and want to check it's functionality like this:
it("should show the second (.slidingImg img) element after testHost.data[0].time seconds
have passed (which here, is 2 seconds)", () => {
//testing
});
i tried many things that i found in docs or anywhere on the internet, but none of them work for me. the problem is that I can't mock the passage of time in a way that the observable timer would perform next actions, and it's like no time has passed whatsoever. two of the ways that haven't worked for me are these:
it("should show the second (.slidingImg img) element after testHost.data[0].time seconds
have passed (which here, is 2 seconds)", fakeAsync(() => {
fixture.detectChanges();
tick(2500);
flushMicrotasks();
fixture.detectChanges();
let secondSlidingImg = fixture.debugElement.queryAll(By.css('.slidingImg'))[1].query(By.css('img'));
expect(secondSlidingImg).toBeTruthy();
//error: expected null to be truthy
}));
i got this from angular2 docs.
and:
beforeEach(() => {
fixture = TestBed.createComponent(TestHostComponent);
testHost = fixture.componentInstance;
scheduler = new TestScheduler((a, b) => expect(a).toEqual(b));
const originalTimer = Observable.interval;
spyOn(Observable, 'interval').and.callFake(function(initialDelay, dueTime) {
return originalTimer.call(this, initialDelay, dueTime, scheduler);
});
// or:
// const originalTimer = Observable.timer;
// spyOn(Observable, 'timer').and.callFake(function(initialDelay, dueTime) {
// return originalTimer.call(this, initialDelay, dueTime, scheduler);
// });
scheduler.maxFrames = 5000;
});
it("should show the second (.slidingImg img) element after testHost.data[0].time seconds
have passed (which here, is 2 seconds)", async(() => {
scheduler.schedule(() => {
fixture.detectChanges();
let secondSlidingImg = fixture.debugElement.queryAll(By.css('.slidingImg'))[1].query(By.css('img'));
expect(secondSlidingImg).toBeTruthy();
//error: expected null to be truthy
}, 2500, null);
scheduler.flush();
}));
i got this approach from this question.
so I desperately need to know how exactly should I simulate time passage in my unit test so that the component's observable time interval would really trigger...
versions:
angular: "2.4.5"
"rxjs": "5.0.1"
"jasmine": "~2.4.1"
"karma": "^1.3.0"
"typescript": "~2.0.10"
"webpack": "2.2.1"
fakeAsync doesn't work for some case with RxJs. You need to manually move internal timer in RxJs. Something along those lines:
import {async} from 'rxjs/internal/scheduler/async';
...
describe('faking internal RxJs scheduler', () => {
let currentTime: number;
beforeEach(() => {
currentTime = 0;
spyOn(async, 'now').and.callFake(() => currentTime);
});
it('testing RxJs delayed execution after 1000ms', fakeAsync(() => {
// Do your stuff
fixture.detectChanges();
currentTime = 1000;
tick(1000);
discardPeriodicTasks();
expect(...);
}));
});
Best way I found for testing this is Marble Testing:
Tutorial: https://medium.com/#bencabanes/marble-testing-observable-introduction-1f5ad39231c
You can control emits by order and time, which seems to be exactly what you need.

AWS javascript SDK request.js send request function execution time gradually increases

I am using aws-sdk to push data to Kinesis stream.
I am using PutRecord to achieve realtime data push.
I am observing same delay in putRecords as well in case of batch write.
I have tried out this with 4 records where I am not crossing any shard limit.
Below is my node js http agent configurations. Default maxSocket value is set to infinity.
Agent {
domain: null,
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: { path: null },
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256 }
Below is my code.
I am using following code to trigger putRecord call
event.Records.forEach(function(record) {
var payload = new Buffer(record.kinesis.data, 'base64').toString('ascii');
// put record request
evt = transformEvent(payload );
promises.push(writeRecordToKinesis(kinesis, streamName, evt ));
}
Event structure is
evt = {
Data: new Buffer(JSON.stringify(payload)),
PartitionKey: payload.PartitionKey,
StreamName: streamName,
SequenceNumberForOrdering: dateInMillis.toString()
};
This event is used in put request.
function writeRecordToKinesis(kinesis, streamName, evt ) {
console.time('WRITE_TO_KINESIS_EXECUTION_TIME');
var deferred = Q.defer();
try {
kinesis.putRecord(evt , function(err, data) {
if (err) {
console.warn('Kinesis putRecord %j', err);
deferred.reject(err);
} else {
console.log(data);
deferred.resolve(data);
}
console.timeEnd('WRITE_TO_KINESIS_EXECUTION_TIME');
});
} catch (e) {
console.error('Error occured while writing data to Kinesis' + e);
deferred.reject(e);
}
return deferred.promise;
}
Below is output for 3 messages.
WRITE_TO_KINESIS_EXECUTION_TIME: 2026ms
WRITE_TO_KINESIS_EXECUTION_TIME: 2971ms
WRITE_TO_KINESIS_EXECUTION_TIME: 3458ms
Here we can see gradual increase in response time and function execution time.
I have added counters in aws-sdk request.js class. I can see same pattern in there as well.
Below is code snippet for aws-sdk request.js class which executes put request.
send: function send(callback) {
console.time('SEND_REQUEST_TO_KINESIS_EXECUTION_TIME');
if (callback) {
this.on('complete', function (resp) {
console.timeEnd('SEND_REQUEST_TO_KINESIS_EXECUTION_TIME');
callback.call(resp, resp.error, resp.data);
});
}
this.runTo();
return this.response;
},
Output for send request:
SEND_REQUEST_TO_KINESIS_EXECUTION_TIME: 1751ms
SEND_REQUEST_TO_KINESIS_EXECUTION_TIME: 1816ms
SEND_REQUEST_TO_KINESIS_EXECUTION_TIME: 2761ms
SEND_REQUEST_TO_KINESIS_EXECUTION_TIME: 3248ms
Here you can see it is increasing gradually.
Can anyone please suggest how can I reduce this delay?
3 seconds to push single record to Kinesis is not at all acceptable.