private void FulfillPromises(List <IPlayFabEmitEventRequest> batch, PlayFabResult <WriteEventsResponse> playFabResult) { PlayFabEmitEventRequest eventRequest; for (int i = 0; i < batch.Count; i++) { // only fulfill given promises eventRequest = (PlayFabEmitEventRequest)batch[i]; if (eventRequest.ResultPromise != null) { var result = new PlayFabEmitEventResponse { Event = eventRequest.Event, EmitEventResult = EmitEventResult.Success, WriteEventsResponse = playFabResult.Result, PlayFabError = playFabResult.Error, Batch = batch }; eventRequest.ResultPromise.SetResult(result); } } }
public async void EmitLightweightEventsAsync(UUnitTestContext testContext) #endif { // create and set settings for OneDS event pipeline var settings = new OneDSEventPipelineSettings(); settings.BatchSize = 8; settings.BatchFillTimeout = TimeSpan.FromSeconds(1); var logger = new DebugLogger(); // create OneDS event pipeline var oneDSPipeline = new OneDSEventPipeline(settings, logger); // create custom event API, add the pipeline var playFabEventApi = new PlayFabEventAPI(logger); #pragma warning disable 4014 playFabEventApi.EventRouter.AddAndStartPipeline(EventPipelineKey.OneDS, oneDSPipeline); #pragma warning restore 4014 // create and emit many lightweight events #if TPL_35 var results = new List <Task <IPlayFabEmitEventResponse> >(); #else var results = new List <Task>(); #endif for (int i = 0; i < 50; i++) { results.AddRange(playFabEventApi.EmitEvent(CreateSamplePlayFabEvent("Event_Custom", PlayFabEventType.Lightweight))); } // wait when the pipeline finishes sending all events #if TPL_35 Task.WhenAll(results).Await(); #else await Task.WhenAll(results); #endif // check results var sentBatches = new Dictionary <IList <IPlayFabEmitEventRequest>, int>(); foreach (var result in results) { testContext.True(result.IsCompleted, "Custom event emission task failed to complete"); PlayFabEmitEventResponse response = (PlayFabEmitEventResponse)((Task <IPlayFabEmitEventResponse>)result).Result; testContext.True(response.EmitEventResult == EmitEventResult.Success, "Custom event emission task failed to succeed"); testContext.True(response.PlayFabError == null && response.WriteEventsResponse != null, "Custom event failed to be sent"); if (!sentBatches.ContainsKey(response.Batch)) { sentBatches[response.Batch] = 0; } sentBatches[response.Batch]++; } int event8 = 0; int event2 = 0; foreach (var batch in sentBatches) { if (batch.Value == 8) { event8++; } else if (batch.Value == 2) { event2++; } } // 6 full batches of 8 events and 1 incomplete batch of 2 events are expected testContext.True(event8 == 6, "Wrong number of full batches"); testContext.True(event2 == 1, "Wrong number of incomplete batches"); testContext.EndTest(UUnitFinishState.PASSED, null); }