public async Task TestLocalAnalytics() { int eventCount = 10000; // Create a LocalAnalytics instance that uses only memory stores for testing: LocalAnalytics localAnalytics = new LocalAnalytics(new InMemoryKeyValueStore()); localAnalytics.createStoreFor = (_) => new InMemoryKeyValueStore().GetTypeAdapter <AppFlowEvent>(); // Pass this local analytics system to the app flow impl. as the target store: AppFlowToStore appFlow = new AppFlowToStore(localAnalytics); await TestAppFlowWithStore(eventCount, appFlow); // Run the tests // Get the store that contains only the events of a specific category: var catMethodStore = localAnalytics.GetStoreForCategory(EventConsts.catMethod); { // Check that all events so far are of the method category: var all = await localAnalytics.GetAll(); var allForCat = await catMethodStore.GetAll(); Assert.Equal(all.Count(), allForCat.Count()); } { // Add an event of a different category and check that the numbers again: appFlow.TrackEvent(EventConsts.catUi, "Some UI event"); var all = await localAnalytics.GetAll(); var allForCat = await catMethodStore.GetAll(); Assert.Equal(all.Count(), allForCat.Count() + 1); var catUiStore = localAnalytics.GetStoreForCategory(EventConsts.catUi); Assert.Single(await catUiStore.GetAll()); } }
private async Task TestAppFlowWithStore(int eventCount, AppFlowToStore appFlow) { Assert.Empty(await appFlow.store.GetAllKeys()); var t1 = Log.MethodEntered($"Track {eventCount} events via appFlow.TrackEvent"); for (int i = 0; i < eventCount; i++) { appFlow.TrackEvent(EventConsts.catMethod, "action " + i); } Log.MethodDone(t1); var t2 = Log.MethodEntered("GetAll events in store"); var events = await appFlow.store.GetAll(); Assert.Equal(eventCount, events.Count()); Log.MethodDone(t2); var t3 = Log.MethodEntered("GetAll again but for specific category"); var filtered = events.Filter(x => x.cat == EventConsts.catMethod); Assert.Equal(filtered, events); Assert.Empty(events.Filter(x => x.cat == EventConsts.catUi)); Log.MethodDone(t3); }
public async Task TestAppFlowToStore() { int eventCount = 1000; var s1 = Log.MethodEntered("InMemoryKeyValueStore"); { IKeyValueStore store = new InMemoryKeyValueStore(); AppFlowToStore appFlow = new AppFlowToStore(store.GetTypeAdapter <AppFlowEvent>()); await TestAppFlowWithStore(eventCount, appFlow); // Run the tests Log.MethodDone(s1, maxAllowedTimeInMs: 1000); } var s2 = Log.MethodEntered("InMemory FileSystem"); { var dir1 = EnvironmentV2.instance.GetNewInMemorySystem().GetChildDir("TestAppFlowToFiles"); IKeyValueStore store = new FileBasedKeyValueStore(dir1.CreateV2()); AppFlowToStore appFlow = new AppFlowToStore(store.GetTypeAdapter <AppFlowEvent>()); await TestAppFlowWithStore(eventCount, appFlow); // Run the tests Assert.Equal(eventCount, dir1.GetFiles().Count()); Log.MethodDone(s2, maxAllowedTimeInMs: 10000); } var s3 = Log.MethodEntered("Real FileSystem"); { var dir2 = EnvironmentV2.instance.GetRootTempFolder().GetChildDir("TestAppFlowToFiles"); dir2.DeleteV2(); // Cleanup from last test IKeyValueStore store = new FileBasedKeyValueStore(dir2.CreateV2()); AppFlowToStore appFlow = new AppFlowToStore(store.GetTypeAdapter <AppFlowEvent>()); await TestAppFlowWithStore(eventCount, appFlow); // Run the tests Assert.Equal(eventCount, dir2.GetFiles().Count()); Log.MethodDone(s3, maxAllowedTimeInMs: 10000); } float ratioInMemVsReadFs = s2.ElapsedMilliseconds / (float)s3.ElapsedMilliseconds; Assert.True(0.01 < ratioInMemVsReadFs && ratioInMemVsReadFs < 1, $"s2={s2} > s3={s3}, ratioInMemVsReadFs={ratioInMemVsReadFs}"); }