public async Task Test1()
        {
            var epsilon = TimeSpan.FromSeconds(0.5);

            await using var serving = await WebSocketHost.ServeAsync();

            var client = Services.GetRequiredService <IClientTimeService>();
            var cTime  = await Computed.CaptureAsync(_ => client.GetTimeAsync());

            cTime.Options.AutoInvalidateTime.Should().Be(ComputedOptions.Default.AutoInvalidateTime);
            if (!cTime.IsConsistent())
            {
                cTime = await cTime.UpdateAsync(false);

                cTime.IsConsistent().Should().BeTrue();
            }
            (DateTime.Now - cTime.Value).Should().BeLessThan(epsilon);

            await TestEx.WhenMet(
                () => cTime.IsConsistent().Should().BeFalse(),
                TimeSpan.FromSeconds(5));

            var time = await cTime.UseAsync();

            (DateTime.Now - time).Should().BeLessThan(epsilon);
        }
        public async Task AutoRecomputeTest()
        {
            var time = Services.GetRequiredService <ITimeService>();
            var c    = await Computed.CaptureAsync(
                _ => time.GetTimeWithOffsetAsync(TimeSpan.FromSeconds(1)));

            var count = 0L;

            using var state = StateFactory.NewLive <DateTime>(
                      o => o.WithZeroUpdateDelay(),
                      async(_, ct) => await c.UseAsync(ct));
            state.Updated += s
                             => Log.LogInformation($"{++count} -> {s.Value:hh:mm:ss:fff}");

            await TestEx.WhenMet(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));

            var lastCount = count;

            state.Dispose();

            await Task.Delay(1000);

            count.Should().Be(lastCount);
        }
        public async Task TimerTest()
        {
            await using var serving = await WebHost.Serve();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();
            var tp         = WebServices.GetRequiredService <ITimeService>();

            var pub = await publisher.Publish(_ => tp.GetTime());

            var rep = replicator.GetOrAdd <DateTime>(pub.Ref);

            var count = 0;

            using var state = Services.StateFactory().NewLive <DateTime>(
                      o => o.WithInstantUpdates(),
                      async(_, ct) => await rep.Computed.Use(ct));
            state.Updated += (s, _) => {
                Out.WriteLine($"{s.Value}");
                count++;
            };

            await TestEx.WhenMet(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }
            public void When_IncludeProperties_output_includes_properties_except_those_defined_on_exception()
            {
                TestEx.Print(includeProperties: true).Should().Be(
                    @"CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
Properties:
  SomeProperty: Some property value");
            }
            public void When_IncludeStackTrace_output_includes_stack_trace()
            {
                TestEx.Print(includeStackTrace: true).Should().StartWith(
                    @"CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
StackTrace:
  at CommandDotNet.Tests.UnitTests.Extensions.TestException.ThrowEx() in CommandDotNet.Tests/UnitTests/Extensions/TestException.cs:line 46");
            }
        public async Task TimerTest()
        {
            await using var serving = await WebSocketHost.ServeAsync();

            var tp = Services.GetRequiredService <ITimeService>();

            var pub = await Publisher.PublishAsync(_ => tp.GetTimeAsync());

            var rep = Replicator.GetOrAdd <DateTime>(pub.Ref);
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1));

            var count = 0;

            using var state = StateFactory.NewLive <DateTime>(
                      o => o.WithZeroUpdateDelay(),
                      async(_, ct) => await rep.Computed.UseAsync(ct));
            state.Updated += s => {
                Out.WriteLine($"Client: {s.Value}");
                count++;
            };

            await TestEx.WhenMet(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }
Beispiel #7
0
            public void When_IncludeData_output_includes_values_from_Data_collection()
            {
                TestEx.Print(includeData: true).Should().Be(
                    @"CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
Data:
  data-key: data value");
            }
Beispiel #8
0
        public async Task TimerTest()
        {
            await using var serving = await WebHost.Serve();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();
            var tp         = WebServices.GetRequiredService <ITimeService>();

            var pub = await publisher.Publish(_ => tp.GetTime());

            var rep = replicator.GetOrAdd <DateTime>(pub.Ref);
            await rep.RequestUpdate().AsAsyncFunc()
            .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1));

            var count = 0;

            using var state = WebServices.StateFactory().NewComputed <DateTime>(
                      UpdateDelayer.ZeroUpdateDelay,
                      async(_, ct) => await rep.Computed.Use(ct));
            state.Updated += (s, _) => {
                Out.WriteLine($"Client: {s.Value}");
                count++;
            };

            await TestEx.WhenMet(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }
Beispiel #9
0
        private static void ValidateMarshalAPIs(bool validateUseRegistered)
        {
            string scenario = validateUseRegistered ? "use registered wrapper" : "fall back to runtime";

            Console.WriteLine($"Running {nameof(ValidateMarshalAPIs)}: {scenario}...");

            GlobalComWrappers registeredWrapper = GlobalComWrappers.Instance;

            registeredWrapper.ReturnInvalid = !validateUseRegistered;

            Console.WriteLine($" -- Validate Marshal.GetIUnknownForObject...");

            var    testObj     = new Test();
            IntPtr comWrapper1 = Marshal.GetIUnknownForObject(testObj);

            Assert.AreNotEqual(IntPtr.Zero, comWrapper1);
            Assert.AreEqual(testObj, registeredWrapper.LastComputeVtablesObject, "Registered ComWrappers instance should have been called");

            IntPtr comWrapper2 = Marshal.GetIUnknownForObject(testObj);

            Assert.AreEqual(comWrapper1, comWrapper2);

            Marshal.Release(comWrapper1);
            Marshal.Release(comWrapper2);

            Console.WriteLine($" -- Validate Marshal.GetIDispatchForObject...");

            Assert.Throws <InvalidCastException>(() => Marshal.GetIDispatchForObject(testObj));

            if (validateUseRegistered)
            {
                var    dispatchObj     = new TestEx(IID_IDISPATCH);
                IntPtr dispatchWrapper = Marshal.GetIDispatchForObject(dispatchObj);
                Assert.AreNotEqual(IntPtr.Zero, dispatchWrapper);
                Assert.AreEqual(dispatchObj, registeredWrapper.LastComputeVtablesObject, "Registered ComWrappers instance should have been called");
            }

            Console.WriteLine($" -- Validate Marshal.GetObjectForIUnknown...");

            IntPtr trackerObjRaw = MockReferenceTrackerRuntime.CreateTrackerObject();
            object objWrapper1   = Marshal.GetObjectForIUnknown(trackerObjRaw);

            Assert.AreEqual(validateUseRegistered, objWrapper1 is FakeWrapper, $"GetObjectForIUnknown should{(validateUseRegistered ? string.Empty : "not")} have returned {nameof(FakeWrapper)} instance");
            object objWrapper2 = Marshal.GetObjectForIUnknown(trackerObjRaw);

            Assert.AreEqual(objWrapper1, objWrapper2);

            Console.WriteLine($" -- Validate Marshal.GetUniqueObjectForIUnknown...");

            object objWrapper3 = Marshal.GetUniqueObjectForIUnknown(trackerObjRaw);

            Assert.AreEqual(validateUseRegistered, objWrapper3 is FakeWrapper, $"GetObjectForIUnknown should{(validateUseRegistered ? string.Empty : "not")} have returned {nameof(FakeWrapper)} instance");

            Assert.AreNotEqual(objWrapper1, objWrapper3);

            Marshal.Release(trackerObjRaw);
        }
Beispiel #10
0
        public ActionResult <IEnumerable <string> > Get()
        {
            var person = new Person {
                Age = 11, Name = "Hiepdeptrai", Name1 = "safd", Name2 = "safd", Name3 = "safd", Name4 = "safd", Name5 = "safd"
            };
            var ex = new TestEx("Exception herer!!!!")
            {
                Person = person,
            };

            logger.LogError(ex, "some person here {@person}", ex.Person);
            return(new string[] { "value1", "value2" });
        }
         public void Honors_indent_when_provided()
         {
             TestEx.Print(indent: new Indent(depth: 2), includeProperties: true, includeData: true, includeStackTrace: true)
             .Should().StartWith(
                 @"    CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
 Properties:
   SomeProperty: Some property value
 Data:
   data-key: data value
   non-serializable-key: non-serializable-value
 StackTrace:
   at CommandDotNet.Tests.UnitTests.Extensions.TestException.ThrowEx() in CommandDotNet.Tests/UnitTests/Extensions/TestException.cs:line 46");
         }
            public void When_include_all_output_includes_stack_trace()
            {
                TestEx.Print(includeProperties: true, includeData: true, includeStackTrace: true)
                .Should().StartWith(
                    @"CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
Properties:
  SomeProperty: Some property value
Data:
  data-key: data value
  non-serializable-key: non-serializable-value
StackTrace:
  at CommandDotNet.Tests.UnitTests.Extensions.TestException.ThrowEx() in CommandDotNet.Tests/UnitTests/Extensions/TestException.cs:line 46");
            }
Beispiel #13
0
        public async Task BasicTest()
        {
            var kv = Services.GetRequiredService <IKeyValueService <string> >();

            (await kv.TryGetAsync("")).Should().Be(Option.None <string>());
            (await kv.GetAsync("")).Should().BeNull();
            await kv.SetAsync("", "1");

            (await kv.TryGetAsync("")).Should().Be(Option.Some("1"));
            (await kv.GetAsync("")).Should().Be("1");

            await using var serving = await WebSocketHost.ServeAsync();

            using var kvm = Services.GetRequiredService <ILiveState <KeyValueModel <string> > >();
            var kvc = Services.GetRequiredService <IKeyValueServiceClient <string> >();

            // First read
            var c = kvm.Computed;

            c.IsConsistent().Should().BeFalse();
            c.Value.Key.Should().Be("");
            c.Value.Value.Should().BeNull();
            c.Value.UpdateCount.Should().Be(0);

            await TestEx.WhenMetAsync(() => {
                var snapshot = kvm.Snapshot;
                snapshot.Computed.HasValue.Should().BeTrue();
                var c = snapshot.Computed;
                c.IsConsistent().Should().BeTrue();
                c.Value.Key.Should().Be("");
                c.Value.Value.Should().Be("1");
                c.Value.UpdateCount.Should().Be(1);
            }, TimeSpan.FromSeconds(1));

            // Update
            await kvc.SetAsync(kvm.Computed.Value.Key, "2");

            await Task.Delay(300);

            c = kvm.Computed;
            c.IsConsistent().Should().BeFalse();
            c.Value.Value.Should().Be("1");
            c.Value.UpdateCount.Should().Be(1);

            await Task.Delay(1000);

            c = kvm.Computed;
            c.IsConsistent().Should().BeTrue();
            c.Value.Value.Should().Be("2");
            c.Value.UpdateCount.Should().Be(2);
        }
Beispiel #14
0
            public void Prints_to_console_when_provided()
            {
                var testConsole = new TestConsole();

                TestEx.Print(testConsole, includeProperties: true, includeData: true, includeStackTrace: true);
                testConsole.AllText().Should().StartWith(
                    @"CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception
Properties:
  SomeProperty: Some property value
Data:
  data-key: data value
StackTrace:
  at CommandDotNet.Tests.UnitTests.Extensions.TestException.ThrowEx() in CommandDotNet.Tests/UnitTests/Extensions/TestException.cs:line 37
  at CommandDotNet.Tests.UnitTests.Extensions.TestException.Build() in CommandDotNet.Tests/UnitTests/Extensions/TestException.cs:line 23");
            }
        public async Task BasicTest()
        {
            if (OSInfo.Kind == OSKind.Unix)
            {
                // Screenshots don't work on Unix
                return;
            }

            var c = await GetScreenshotComputedAsync();

            for (var i = 0; i < 10; i++)
            {
                c.Value.Base64Content.Length.Should().BeGreaterThan(0);
                await TestEx.WhenMetAsync(
                    () => c.IsConsistent().Should().BeFalse(),
                    TimeSpan.FromSeconds(0.5));

                c = await GetScreenshotComputedAsync();
            }
        }
 public void Default_prints_only_the_exception_message()
 {
     TestEx.Print().Should().Be(
         "CommandDotNet.Tests.UnitTests.Extensions.TestException: I'm a test exception");
 }