Exemplo n.º 1
0
 public void Should_replay_last_event_when_adding_a_channel()
 {
     // given
     var multicastChannel = new MulticastChannel();
     var channel = Substitute.For<IEventChannel>();
     multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None);
     // when
     multicastChannel.AddChannel(channel, CancellationToken.None);
     // then
     channel.Received().Send(Arg.Any<ServerSentEvent>(), CancellationToken.None);
 }
Exemplo n.º 2
0
        public void Should_replay_last_event_when_adding_a_channel()
        {
            // given
            var multicastChannel = new MulticastChannel();
            var channel          = Substitute.For <IEventChannel>();

            multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None);
            // when
            multicastChannel.AddChannel(channel, CancellationToken.None);
            // then
            channel.Received().Send(Arg.Any <ServerSentEvent>(), CancellationToken.None);
        }
Exemplo n.º 3
0
 public void Should_stop_sending_events_on_a_close_channel()
 {
     // given
     var multicastChannel = new MulticastChannel();
     var channel = Substitute.For<IEventChannel>();
     channel.When(c => c.Send(Arg.Any<ServerSentEvent>(), CancellationToken.None)).Do(x => { throw new Exception(); });
     multicastChannel.AddChannel(channel, CancellationToken.None);
     // when
     multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None); // exception raised
     multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None); // channel should be removed
     // then
     channel.Received(1).Send(Arg.Any<ServerSentEvent>(), CancellationToken.None);
 }
Exemplo n.º 4
0
 public void Should_replay_only_last_events_when_adding_a_channel()
 {
     // given
     var multicastChannel = new MulticastChannel(2);
     var channel = Substitute.For<IEventChannel>();
     multicastChannel.Send(new ServerSentEvent("DEBUG", "first"), CancellationToken.None);
     multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever2"), CancellationToken.None);
     multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever3"), CancellationToken.None);
     // when
     multicastChannel.AddChannel(channel, CancellationToken.None);
     // then
     channel.Received(2).Send(Arg.Any<ServerSentEvent>(), CancellationToken.None);
     channel.DidNotReceive().Send(Arg.Is<ServerSentEvent>(e => e.ToString().Contains("first")), CancellationToken.None);
 }
Exemplo n.º 5
0
        public void Should_stop_sending_events_on_a_close_channel()
        {
            // given
            var multicastChannel = new MulticastChannel();
            var channel          = Substitute.For <IEventChannel>();

            channel.When(c => c.Send(Arg.Any <ServerSentEvent>(), CancellationToken.None)).Do(x => { throw new Exception(); });
            multicastChannel.AddChannel(channel, CancellationToken.None);
            // when
            multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None); // exception raised
            multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever"), CancellationToken.None); // channel should be removed
            // then
            channel.Received(1).Send(Arg.Any <ServerSentEvent>(), CancellationToken.None);
        }
Exemplo n.º 6
0
        public void Should_replay_only_last_events_when_adding_a_channel()
        {
            // given
            var multicastChannel = new MulticastChannel(2);
            var channel          = Substitute.For <IEventChannel>();

            multicastChannel.Send(new ServerSentEvent("DEBUG", "first"), CancellationToken.None);
            multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever2"), CancellationToken.None);
            multicastChannel.Send(new ServerSentEvent("DEBUG", "whatever3"), CancellationToken.None);
            // when
            multicastChannel.AddChannel(channel, CancellationToken.None);
            // then
            channel.Received(2).Send(Arg.Any <ServerSentEvent>(), CancellationToken.None);
            channel.DidNotReceive().Send(Arg.Is <ServerSentEvent>(e => e.ToString().Contains("first")), CancellationToken.None);
        }
Exemplo n.º 7
0
        public virtual IEventChannel Create(string host, int port, int bufferSize)
        {
            var channel = new MulticastChannel(bufferSize);

            var jsTemplate = GetContent(Assembly.GetExecutingAssembly().GetManifestResourceStream("BrowserLog.BrowserLog.js"));
            var htmlTemplate = GetContent(Assembly.GetExecutingAssembly().GetManifestResourceStream("BrowserLog.homepage.html"));
            
            var html = htmlTemplate.Replace("HOST", Dns.GetHostName()).Replace("PORT", port.ToString());

            Action<HttpContext> handler = ctx =>
            {
                var httpResponse = new HttpResponse(200, "OK");
                if (ctx.HttpRequest.Uri == "/")
                {
                    httpResponse.AddHeader("Content-Type", "text/html");
                    httpResponse.AddHeader("Connection", "close");
                    httpResponse.Content = html;
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token).ContinueWith(t => ctx.ResponseChannel.Close());
                } 
                else if (ctx.HttpRequest.Uri.Contains(".js"))
                {
                    var js = jsTemplate.Replace("URL_QUERY", ctx.HttpRequest.Uri);

                    httpResponse.AddHeader("Content-Type", "text/javascript");
                    httpResponse.AddHeader("Connection", "close");
                    httpResponse.Content = js;
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token).ContinueWith(t => ctx.ResponseChannel.Close());
                }
                else
                {
                    httpResponse.AddHeader("Content-Type", "text/event-stream");
                    httpResponse.AddHeader("Cache-Control", "no-cache");
                    httpResponse.AddHeader("Connection", "keep-alive");
                    httpResponse.AddHeader("Access-Control-Allow-Origin", "*");
                    ctx.ResponseChannel.Send(httpResponse, ctx.Token)
                        .ContinueWith(t =>
                        {
                            ctx.ResponseChannel.Send(new ServerSentEvent("INFO",
                                "Connected successfully on LOG stream from " + host + ":" + port), ctx.Token);
                            channel.AddChannel(ctx.ResponseChannel, ctx.Token);
                        });      
                }
            };
            var httpServer = new HttpServer(host, port, handler);
            channel.AttachServer(httpServer);
            httpServer.Run();
            return channel;
        }