Пример #1
0
        public static void ShowMeHowToKeepAnUpdatedState(
            string stockMarketApiTestUrl,
            string secretKey)
        {
            var client = StreamdataClient <StockMarketOrders>
                         .WithConfiguration(conf => {
                conf.SecretKey = secretKey;
                conf.KeepStateUpdated();
            });

            int consoleWidth = 48;
            int counter      = 0;

            StockMarketOrders actual = new StockMarketOrders();

            client.OnUpdatedState += (orders) => {
                Clear();
                counter++;
                PrintOrders(consoleWidth, counter, orders, actual);
                WriteLine("...press [esc] to quit app");
                actual = orders.Clone();
            };

            client.Start(stockMarketApiTestUrl);

            while ((Console.ReadKey().Key) != ConsoleKey.Escape)
            {
                client.Stop();
                Console.WriteLine("stopped...");
            }
        }
Пример #2
0
        WHEN_client_recieves_data_THEN_it_should_call_ondata()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            StockMarketOrders testData             = null;
            StockMarketOrders expectedMarketOrders = new StockMarketOrders();

            expectedMarketOrders.Add(new Order()
            {
                Price = 10, Title = "test1"
            });
            expectedMarketOrders.Add(new Order()
            {
                Price = 20, Title = "test2"
            });

            client.OnData(data => testData = data);

            client.Start("fakeurl");

            FakeEngineWrapper.Instance.SendData(expectedMarketOrders);

            Check.That(testData).IsNotNull();
            Check.That(testData.GetTotal()).IsEqualTo(30);
        }
Пример #3
0
        WHEN_client_recieves_patch_THEN_it_should_call_onpatch_and_updateddata()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            StockMarketOrders testData             = null;
            StockMarketOrders expectedMarketOrders = new StockMarketOrders();

            expectedMarketOrders.Add(new Order()
            {
                Price = 10, Title = "test1"
            });
            expectedMarketOrders.Add(new Order()
            {
                Price = 20, Title = "test2"
            });

            client.OnData(data => testData = data);
            client.OnPatch(patch => patch.ApplyTo(testData));
            client.Start("fakeurl");

            FakeEngineWrapper.Instance.SendData(expectedMarketOrders);
            var patchDoc = new JsonPatchDocument <StockMarketOrders>(new List <Operation <StockMarketOrders> >
            {
                new Operation <StockMarketOrders>("replace", "0/price", "0/price", 30)
            });

            FakeEngineWrapper.Instance.SendPatch(patchDoc);
            Check.That(testData).IsNotNull();
            Check.That(testData).HasSize(2);
            Check.That(testData.GetTotal()).IsEqualTo(50);
        }
Пример #4
0
        WHEN_client_has_data_and_patch_THEN_state_is_updated()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
                conf.KeepStateUpdated();
            });

            StockMarketOrders expectedMarketOrders = new StockMarketOrders();

            expectedMarketOrders.Add(new Order()
            {
                Price = 10, Title = "test1"
            });
            expectedMarketOrders.Add(new Order()
            {
                Price = 20, Title = "test2"
            });
            StockMarketOrders actualState = null;

            client.Start("fakeurl");
            client.OnUpdatedState += state => actualState = state;
            FakeEngineWrapper.Instance.SendData(expectedMarketOrders);
            var patch = new JsonPatchDocument <StockMarketOrders>(new List <Operation <StockMarketOrders> >
            {
                new Operation <StockMarketOrders>("replace", "0/price", "0/price", 20)
            });

            FakeEngineWrapper.Instance.SendPatch(patch);
            Check.That(actualState).IsNotNull();
            Check.That(actualState.GetTotal()).IsEqualTo(40);
        }
Пример #5
0
 WHEN_I_create_new_instance_in_sandbox_mode_THEN_ensure_secretkey_is_not_needed()
 {
     Check.ThatCode(() =>
     {
         var client = StreamdataClient <FakeObject> .WithConfiguration(conf =>
         {
             conf.UseSandbox();
         });
     }).DoesNotThrow();
 }
Пример #6
0
        WHEN_I_want_to_create_new_instance_THEN_i_must_use_configuration_helper()
        {
            var client = StreamdataClient <FakeObject> .WithConfiguration(conf =>
            {
                conf.ApiUrl = "http://fakeurl";
                conf.UseSandbox();
            });

            Check.That(client.Configuration.ApiUrl).IsEqualTo("http://fakeurl");
        }
Пример #7
0
        WHEN_configuration_is_used_THEN_ensure_it_can_build_the_right_url()
        {
            ConfigurationManager.AppSettings["streamdata:secretkey"] = "abc";
            var client = StreamdataClient <FakeObject> .WithConfiguration(
                conf => conf.UserServerSentEventEngine <FakeEngine>());

            string url      = client.Configuration.BuildUrl("fakeurl");
            string expected = $"{StreamdataOfficialUrls.PRODUCTION}/fakeurl?X-Sd-Token=abc";

            client.Start("fakeurl");
            Check.That(client.ListenUrl).IsEqualTo(expected);
        }
Пример #8
0
 WHEN_I_create_new_instance_in_production_mode_THEN_ensure_secretkey_is_configured()
 {
     ConfigurationManager.AppSettings["streamdata:secretkey"] = null;
     Check.ThatCode(() =>
     {
         var client = StreamdataClient <FakeObject> .WithConfiguration(conf =>
         {
             conf.UseProduction();
         });
     }).Throws <StreamdataConfigurationException>()
     .WithMessage("[SecretKey] not configured");
 }
Пример #9
0
        WHEN_client_starts_THEN_engine_apiurl_should_be_set()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            client.Start("fakeurl");

            Check.That(client.Configuration.ApiUrl).IsEqualTo("fakeurl");
        }
Пример #10
0
 WHEN_I_stop_client_THEN_ensure_engine_does_not_throw()
 {
     Check.ThatCode(() =>
     {
         var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
         {
             conf.UseSandbox();
             conf.UserServerSentEventEngine <FakeEngine>();
         });
         client.Stop();
     }).DoesNotThrow();
 }
Пример #11
0
        WHEN_client_starts_THEN_it_should_order_engine_to_start()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            client.Start("fakeurl");

            Check.That(FakeEngineWrapper.Instance.IsStarted).IsTrue();
        }
Пример #12
0
        WHEN_client_is_stopped_THEN_ensure_engine_is_stopped_before_destruction()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            client.Start("fakeurl");
            Check.ThatCode(() => client.Stop()).DoesNotThrow();

            Check.That(FakeEngineWrapper.Instance.IsStarted).IsFalse();
        }
Пример #13
0
        WHEN_client_has_no_data_THEN_state_is_default_and_dont_throw_exception()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            client.Start("fakeurl");
            StockMarketOrders actualState = null;

            Check.ThatCode(() => actualState = client.State)
            .DoesNotThrow();

            Check.That(actualState).IsEqualTo(default(StockMarketOrders));
        }
Пример #14
0
        public static void ShowMeHowToGetDataAndPatches(
            string stockMarketApiTestUrl)
        {
            var client = StreamdataClient <StockMarketOrders>
                         .WithConfiguration(conf => {
                conf.UseSandbox();
            });

            int consoleWidth = 48;
            int counter      = 0;

            StockMarketOrders previousOrders = new StockMarketOrders();;
            StockMarketOrders actualOrders   = new StockMarketOrders();;

            client.OnData(data => {
                Clear();
                counter++;
                actualOrders = data;
                PrintOrders(consoleWidth, counter, data, previousOrders);
                previousOrders = data.Clone();
            });

            client.OnPatch(patch => {
                Clear();
                counter++;
                patch.ApplyTo(actualOrders);
                PrintOrders(consoleWidth, counter, actualOrders, previousOrders);
                previousOrders = actualOrders.Clone();
                var json       = JsonConvert.SerializeObject(patch);
                WriteLine($"patch:{json}");
            });

            client.Start(stockMarketApiTestUrl);

            while ((Console.ReadKey().Key) != ConsoleKey.Escape)
            {
                client.Stop();
                Console.WriteLine("stopped...");
            }
        }
Пример #15
0
        WHEN_engine_receives_data_without_ondata_configured_THEN_it_should_not_throw_exception()
        {
            var client = StreamdataClient <StockMarketOrders> .WithConfiguration(conf =>
            {
                conf.UseSandbox();
                conf.UserServerSentEventEngine <FakeEngine>();
            });

            StockMarketOrders expectedMarketOrders = new StockMarketOrders();

            expectedMarketOrders.Add(new Order()
            {
                Price = 10, Title = "test1"
            });
            expectedMarketOrders.Add(new Order()
            {
                Price = 20, Title = "test2"
            });

            client.Start("fakeurl");

            Check.ThatCode(() => FakeEngineWrapper.Instance.SendData(expectedMarketOrders)).DoesNotThrow();
        }