コード例 #1
0
        public async Task Test_AddConnectionIdAsync_RemoveConnectionIdAsync()
        {
            var userId       = Guid.NewGuid();
            var connectionId = Guid.NewGuid().ToString("N");

            var actor = new TheActor(CreateActorService(CreateBus(null)), new ActorId(userId));

            actor.IsInUnitTest = true;

            await actor.AddConnectionIdAsync(connectionId);

            var connectionIds = await actorStateManager.GetStateAsync <List <string> >(TheActor.ConnectionIdListPropertyName);

            Assert.IsTrue(connectionIds.Count == 1);  //添加成功

            await actor.RemoveConnectionIdAsync(Guid.NewGuid().ToString("N"));

            connectionIds = await actorStateManager.GetStateAsync <List <string> >(TheActor.ConnectionIdListPropertyName);

            Assert.IsTrue(connectionIds.Count == 1); //没有被删除掉

            await actor.RemoveConnectionIdAsync(connectionId);

            connectionIds = await actorStateManager.GetStateAsync <List <string> >(TheActor.ConnectionIdListPropertyName);

            Assert.IsTrue(connectionIds == null || connectionIds.Count == 0); //删除掉
        }
コード例 #2
0
        public async Task Test_PushMsgNotifyAsync_WhenHaveConnection()
        {
            var userId         = Guid.NewGuid();
            var connectionId   = Guid.NewGuid().ToString("N");
            var conversationId = Guid.NewGuid();
            var msgId          = Guid.NewGuid();

            void configBus(IInMemoryReceiveEndpointConfigurator e)
            {
                e.Handler <SendMessageNotify>(context =>
                {
                    var notify = context.Message;

                    Assert.IsNotNull(notify);
                    Assert.AreEqual(1, notify.Notifies.Count);
                    Assert.AreEqual(conversationId.ToString(), notify.Notifies[0].TargetId);
                    Assert.AreEqual(msgId, notify.Notifies[0].LatestMsgId);
                    Assert.AreEqual(1, notify.Notifies[0].TargetCategory);

                    return(Task.CompletedTask);
                });

                MapEndpointConverion(e);
            };

            var actor = new TheActor(CreateActorService(CreateBus(configBus)), new ActorId(userId));

            actor.IsInUnitTest = true;

            await actor.AddConnectionIdAsync(connectionId);

            await actor.PushMsgNotifyAsync(new MessageNotifyDto
            {
                Target         = NotifyTargetType.Conversation,
                TargetId       = conversationId.ToString(),
                TargetCategory = 1,
                LatestMsgId    = msgId,
            });

            await Task.Delay(500);
        }