Ejemplo n.º 1
0
        private void BuildTelemetrySyncTask()
        {
            var httpClient   = new SplitioHttpClient(ApiKey, _config.HttpConnectionTimeout, GetHeaders());
            var telemetryAPI = new TelemetryAPI(httpClient, _config.TelemetryServiceURL, _telemetryRuntimeProducer);

            _telemetrySyncTask = new TelemetrySyncTask(_telemetryStorageConsumer, telemetryAPI, _splitCache, _segmentCache, _config, FactoryInstantiationsService.Instance(), _wrapperAdapter, _tasksManager);
        }
Ejemplo n.º 2
0
        public void EventSourceClient_SegmentUpdateEvent_ShouldReceiveEvent()
        {
            using (var httpClientMock = new HttpClientMock())
            {
                var notification = "id: 234234432\nevent: message\ndata: {\"id\":\"jSOE7oGJWo:0:0\",\"clientId\":\"pri:ODc1NjQyNzY1\",\"timestamp\":1588254699236,\"encoding\":\"json\",\"channel\":\"xxxx_xxxx_segments\",\"data\":\"{\\\"type\\\":\\\"SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1585868933303,\\\"segmentName\\\":\\\"test-segment\\\"}\"}";
                httpClientMock.SSE_Channels_Response(notification);

                var url            = httpClientMock.GetUrl();
                var eventsReceived = new BlockingCollection <EventReceivedEventArgs>(new ConcurrentQueue <EventReceivedEventArgs>());
                var actionEvent    = new BlockingCollection <SSEActionsEventArgs>(new ConcurrentQueue <SSEActionsEventArgs>());

                var notificationParser       = new NotificationParser();
                var wrapperAdapter           = new WrapperAdapter();
                var sseHttpClient            = new SplitioHttpClient("api-key", 5000);
                var telemetryRuntimeProducer = new InMemoryTelemetryStorage();

                var eventSourceClient = new EventSourceClient(notificationParser, wrapperAdapter, sseHttpClient, telemetryRuntimeProducer, new TasksManager(wrapperAdapter));
                eventSourceClient.EventReceived += delegate(object sender, EventReceivedEventArgs e)
                {
                    eventsReceived.TryAdd(e);
                };
                eventSourceClient.ActionEvent += delegate(object sender, SSEActionsEventArgs e)
                {
                    actionEvent.TryAdd(e);
                };
                eventSourceClient.ConnectAsync(url);

                eventsReceived.TryTake(out EventReceivedEventArgs ev, 10000);
                Assert.AreEqual(NotificationType.SEGMENT_UPDATE, ev.Event.Type);
                Assert.AreEqual(1585868933303, ((SegmentChangeNotification)ev.Event).ChangeNumber);
                Assert.AreEqual("test-segment", ((SegmentChangeNotification)ev.Event).SegmentName);
                actionEvent.TryTake(out SSEActionsEventArgs action, 10000);
                Assert.AreEqual(SSEClientActions.CONNECTED, action.Action);
            }
        }
Ejemplo n.º 3
0
        public void EventSourceClient_KeepAliveResponse()
        {
            using (var httpClientMock = new HttpClientMock())
            {
                httpClientMock.SSE_Channels_Response(":keepalive\n\n");

                var url            = httpClientMock.GetUrl();
                var eventsReceived = new BlockingCollection <EventReceivedEventArgs>(new ConcurrentQueue <EventReceivedEventArgs>());
                var actionEvent    = new BlockingCollection <SSEActionsEventArgs>(new ConcurrentQueue <SSEActionsEventArgs>());

                var notificationParser       = new NotificationParser();
                var wrapperAdapter           = new WrapperAdapter();
                var sseHttpClient            = new SplitioHttpClient("api-key", 5000);
                var telemetryRuntimeProducer = new InMemoryTelemetryStorage();

                var eventSourceClient = new EventSourceClient(notificationParser, wrapperAdapter, sseHttpClient, telemetryRuntimeProducer, new TasksManager(wrapperAdapter));
                eventSourceClient.EventReceived += delegate(object sender, EventReceivedEventArgs e)
                {
                    eventsReceived.TryAdd(e);
                };
                eventSourceClient.ActionEvent += delegate(object sender, SSEActionsEventArgs e)
                {
                    actionEvent.TryAdd(e);
                };
                eventSourceClient.ConnectAsync(url);

                actionEvent.TryTake(out SSEActionsEventArgs action, 10000);
                Assert.AreEqual(SSEClientActions.CONNECTED, action.Action);
                Thread.Sleep(1000);
                Assert.AreEqual(0, eventsReceived.Count);
            }
        }
Ejemplo n.º 4
0
        public void EventSourceClient_NotificationError_ShouldReceiveError()
        {
            using (var httpClientMock = new HttpClientMock())
            {
                var notification = "event: error\ndata: {\"message\":\"Token expired\",\"code\":40142,\"statusCode\":401,\"href\":\"https://help.ably.io/error/40142\"}\n\n";
                httpClientMock.SSE_Channels_Response(notification);

                var url            = httpClientMock.GetUrl();
                var eventsReceived = new BlockingCollection <EventReceivedEventArgs>(new ConcurrentQueue <EventReceivedEventArgs>());
                var actionEvent    = new BlockingCollection <SSEActionsEventArgs>(new ConcurrentQueue <SSEActionsEventArgs>());

                var notificationParser       = new NotificationParser();
                var wrapperAdapter           = new WrapperAdapter();
                var sseHttpClient            = new SplitioHttpClient("api-key", 5000);
                var telemetryRuntimeProducer = new InMemoryTelemetryStorage();

                var eventSourceClient = new EventSourceClient(notificationParser, wrapperAdapter, sseHttpClient, telemetryRuntimeProducer, new TasksManager(wrapperAdapter));
                eventSourceClient.EventReceived += delegate(object sender, EventReceivedEventArgs e)
                {
                    eventsReceived.TryAdd(e);
                };
                eventSourceClient.ActionEvent += delegate(object sender, SSEActionsEventArgs e)
                {
                    actionEvent.TryAdd(e);
                };
                eventSourceClient.ConnectAsync(url);

                actionEvent.TryTake(out SSEActionsEventArgs action, 10000);
                Assert.AreEqual(SSEClientActions.DISCONNECT, action.Action);
            }
        }
Ejemplo n.º 5
0
        public void EventSourceClient_ControlEvent_StreamingDisabled_ShouldReceiveEvent()
        {
            using (var httpClientMock = new HttpClientMock())
            {
                var notification = "id: 234234432\nevent: message\ndata: {\"id\":\"jSOE7oGJWo:0:0\",\"clientId\":\"pri:ODc1NjQyNzY1\",\"timestamp\":1588254699236,\"encoding\":\"json\",\"channel\":\"[?occupancy=metrics.publishers]control_pri\",\"data\":\"{\\\"type\\\":\\\"CONTROL\\\",\\\"controlType\\\":\\\"STREAMING_DISABLED\\\"}\"}";
                httpClientMock.SSE_Channels_Response(notification);

                var url            = httpClientMock.GetUrl();
                var eventsReceived = new BlockingCollection <EventReceivedEventArgs>(new ConcurrentQueue <EventReceivedEventArgs>());
                var actionEvent    = new BlockingCollection <SSEActionsEventArgs>(new ConcurrentQueue <SSEActionsEventArgs>());

                var notificationParser       = new NotificationParser();
                var wrapperAdapter           = new WrapperAdapter();
                var sseHttpClient            = new SplitioHttpClient("api-key", 5000);
                var telemetryRuntimeProducer = new InMemoryTelemetryStorage();

                var eventSourceClient = new EventSourceClient(notificationParser, wrapperAdapter, sseHttpClient, telemetryRuntimeProducer, new TasksManager(wrapperAdapter));
                eventSourceClient.EventReceived += delegate(object sender, EventReceivedEventArgs e)
                {
                    eventsReceived.TryAdd(e);
                };
                eventSourceClient.ActionEvent += delegate(object sender, SSEActionsEventArgs e)
                {
                    actionEvent.TryAdd(e);
                };
                eventSourceClient.ConnectAsync(url);

                eventsReceived.TryTake(out EventReceivedEventArgs ev, 10000);
                Assert.AreEqual(NotificationType.CONTROL, ev.Event.Type);
                Assert.AreEqual(ControlType.STREAMING_DISABLED, ((ControlNotification)ev.Event).ControlType);
                actionEvent.TryTake(out SSEActionsEventArgs action, 10000);
                Assert.AreEqual(SSEClientActions.CONNECTED, action.Action);
            }
        }
Ejemplo n.º 6
0
        private void BuildSyncManager()
        {
            try
            {
                // Synchronizer
                var impressionsCountSender = new ImpressionsCountSender(_impressionsSdkApiClient, _impressionsCounter, _tasksManager);
                var backOff      = new BackOff(backOffBase: 10, attempt: 0, maxAllowed: 60);
                var synchronizer = new Synchronizer(_splitFetcher, _selfRefreshingSegmentFetcher, _impressionsLog, _eventsLog, impressionsCountSender, _wrapperAdapter, _statusManager, _telemetrySyncTask, _tasksManager, _splitCache, backOff, _config.OnDemandFetchMaxRetries, _config.OnDemandFetchRetryDelayMs, _segmentCache);

                // Workers
                var splitsWorker   = new SplitsWorker(_splitCache, synchronizer, _tasksManager);
                var segmentsWorker = new SegmentsWorker(synchronizer, _tasksManager);

                // NotificationProcessor
                var notificationProcessor = new NotificationProcessor(splitsWorker, segmentsWorker);

                // NotificationParser
                var notificationParser = new NotificationParser();

                // NotificationManagerKeeper
                var notificationManagerKeeper = new NotificationManagerKeeper(_telemetryRuntimeProducer);

                // EventSourceClient
                var headers = GetHeaders();
                headers.Add(Constants.Http.SplitSDKClientKey, ApiKey.Substring(ApiKey.Length - 4));
                headers.Add(Constants.Http.Accept, Constants.Http.EventStream);
                var sseHttpClient     = new SplitioHttpClient(ApiKey, _config.HttpConnectionTimeout, headers);
                var eventSourceClient = new EventSourceClient(notificationParser, _wrapperAdapter, sseHttpClient, _telemetryRuntimeProducer, _tasksManager);

                // SSEHandler
                var sseHandler = new SSEHandler(_config.StreamingServiceURL, splitsWorker, segmentsWorker, notificationProcessor, notificationManagerKeeper, eventSourceClient: eventSourceClient);

                // AuthApiClient
                var httpClient    = new SplitioHttpClient(ApiKey, _config.HttpConnectionTimeout, GetHeaders());
                var authApiClient = new AuthApiClient(_config.AuthServiceURL, ApiKey, httpClient, _telemetryRuntimeProducer);

                // PushManager
                var backoff     = new BackOff(_config.AuthRetryBackoffBase, attempt: 1);
                var pushManager = new PushManager(sseHandler, authApiClient, _wrapperAdapter, _telemetryRuntimeProducer, backoff);

                // SyncManager
                _syncManager = new SyncManager(_config.StreamingEnabled, synchronizer, pushManager, sseHandler, notificationManagerKeeper, _telemetryRuntimeProducer, _statusManager, _tasksManager, _wrapperAdapter, _telemetrySyncTask);
            }
            catch (Exception ex)
            {
                _log.Error($"BuildSyncManager: {ex.Message}");
            }
        }
Ejemplo n.º 7
0
        public void EventSourceClient_IncorrectFormat_ShouldReceiveError()
        {
            using (var httpClientMock = new HttpClientMock())
            {
                httpClientMock.SSE_Channels_Response(
                    @"{ 'event': 'message', 
                        'data': {
                            'id':'1',
                            'channel':'mauroc',
                            'content': {
                                'type': 'CONTROL', 
                                'controlType': 'test-control-type'
                            },
                            'name':'name-test'
                         }
                        }");

                var url            = httpClientMock.GetUrl();
                var eventsReceived = new BlockingCollection <EventReceivedEventArgs>(new ConcurrentQueue <EventReceivedEventArgs>());
                var actionEvent    = new BlockingCollection <SSEActionsEventArgs>(new ConcurrentQueue <SSEActionsEventArgs>());

                var notificationParser       = new NotificationParser();
                var wrapperAdapter           = new WrapperAdapter();
                var sseHttpClient            = new SplitioHttpClient("api-key", 5000);
                var telemetryRuntimeProducer = new InMemoryTelemetryStorage();

                var eventSourceClient = new EventSourceClient(notificationParser, wrapperAdapter, sseHttpClient, telemetryRuntimeProducer, new TasksManager(wrapperAdapter));
                eventSourceClient.EventReceived += delegate(object sender, EventReceivedEventArgs e)
                {
                    eventsReceived.TryAdd(e);
                };
                eventSourceClient.ActionEvent += delegate(object sender, SSEActionsEventArgs e)
                {
                    actionEvent.TryAdd(e);
                };
                eventSourceClient.ConnectAsync(url);

                actionEvent.TryTake(out SSEActionsEventArgs action, 10000);
                Assert.AreEqual(SSEClientActions.CONNECTED, action.Action);
                Assert.AreEqual(0, eventsReceived.Count);
            }
        }