コード例 #1
0
    public void Timestamp_現在日時が設定される()
    {
        var now      = DateTimeOffset.Now.Ticks;
        var instance = new ServiceLikeContext("foo");

        instance.Timestamp.Should().BeInRange(now - 30000, now + 30000);
    }
コード例 #2
0
    public void Key_ctorで指定した値が設定される()
    {
        var expect   = "alksjfolawjef";
        var instance = new ServiceLikeContext(expect);

        instance.Key.Should().Be(expect);
    }
コード例 #3
0
    private void PublishAndForget(IEnumerable <MediatorListenerDescription> listeners, object notification, string key, PublishOptions options)
    {
        Task.Run(async() =>
        {
            var context = new ServiceLikeContext(key, options.Header);

            try
            {
                var listenersList = listeners.ToList();
                _logger.TraceStartPublishToListeners(key, listenersList);

                using var scope = _scopedServiceFactoryFactory.Create();

                var contextAccessors = scope.Instance.GetInstances <IServiceLikeContextAccessor>();
                if (contextAccessors.Any())
                {
                    contextAccessors.First().Context = context;
                }

                var publishTasks        = new List <Task>();
                var serviceLikeMediator = new ServiceLikeMediator(scope.Instance);

                if (options.BeforePublishAsyncHandler is not null)
                {
                    await options.BeforePublishAsyncHandler.Invoke(notification, context).ConfigureAwait(false);
                }

                foreach (var listener in listenersList)
                {
                    try
                    {
                        var value = TranslateType(notification, listener.ListenerType);
                        _logger.TracePublishToListener(listener);
                        publishTasks.Add(FireEvent(listener, serviceLikeMediator, value !));
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorOnPublish(ex, listener);
                    }
                }
                // すべて投げたまでで完了とする。
                if (options.CompleteAsyncHandler is not null)
                {
                    await options.CompleteAsyncHandler.Invoke(notification, context).ConfigureAwait(false);
                }
                _logger.TraceFinishPublishToListeners(key);

                await Task.WhenAll(publishTasks);
            }
            catch (Exception e)
            {
                if (options.ErrorAsyncHandler is not null)
                {
                    await options.ErrorAsyncHandler.Invoke(e, notification, context).ConfigureAwait(false);
                }
                _logger.ErrorOnPublishEvents(e, key);
            }
        }).ConfigureAwait(false);
    }
コード例 #4
0
    public async Task Context_設定したコンテキストが取得できる()
    {
        var accessor = new ServiceLikeContextAccessor();
        var context  = new ServiceLikeContext("foo");

        accessor.Context = context;

        await Task.Delay(100);

        context.Should().BeSameAs(accessor.Context);
    }
コード例 #5
0
    public void Header_コンストラクタで指定した内容が設定されている()
    {
        var header = new Dictionary <string, object>()
        {
            { "key1", "123" },
            { "key2", 456 },
        };
        var instance = new ServiceLikeContext("key", header);

        instance.Header.Count().Should().Be(header.Count());
        instance.Header["key1"].Should().Be(header["key1"]);
        instance.Header["key2"].Should().Be(header["key2"]);
    }
コード例 #6
0
    public async Task Context_親のAsyncContextで別のインスタンスが設定されたら子のコンテキストはnullになる()
    {
        var accessor = new ServiceLikeContextAccessor();
        var context  = new ServiceLikeContext("foo");

        accessor.Context = context;

        var checkAsyncFlowTcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
        var waitForNullTcs    = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
        var afterNullCheckTcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

        ThreadPool.QueueUserWorkItem(async _ =>
        {
            context.Should().BeSameAs(accessor.Context);
            checkAsyncFlowTcs.SetResult(null);

            await waitForNullTcs.Task;

            try
            {
                accessor.Context.Should().BeNull();
                afterNullCheckTcs.SetResult(null);
            }
            catch (Exception ex)
            {
                afterNullCheckTcs.SetException(ex);
            }
        });

        await checkAsyncFlowTcs.Task;

        var context2 = new ServiceLikeContext("bar");

        accessor.Context = context2;

        waitForNullTcs.SetResult(null);

        context2.Should().BeSameAs(accessor.Context);

        await afterNullCheckTcs.Task;
    }
コード例 #7
0
    public async Task Context_親のAsyncContextにつながっていない場合は値は設定されない()
    {
        var accessor = new ServiceLikeContextAccessor();
        var context  = new ServiceLikeContext("foo");

        accessor.Context = context;

        var checkAsyncFlowTcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

        ThreadPool.UnsafeQueueUserWorkItem(_ =>
        {
            try
            {
                accessor.Context.Should().BeNull();
                checkAsyncFlowTcs.SetResult(null);
            }
            catch (Exception ex)
            {
                checkAsyncFlowTcs.SetException(ex);
            }
        }, null);

        await checkAsyncFlowTcs.Task;
    }
コード例 #8
0
    public void Id_IdにはGuidベースの値が設定される()
    {
        var instance = new ServiceLikeContext("key");

        Guid.TryParseExact(instance.Id, "N", out var _).Should().BeTrue();
    }
コード例 #9
0
    public void ctor_Headerにnullを設定してもからのHeaderがプロパティから取得できる()
    {
        var instance = new ServiceLikeContext("key", null);

        instance.Header.Count().Should().Be(0);
    }