Example #1
0
        public Task EventHandler(Type observerType, List <BytesBox> list)
        {
            var groups = list.Select(bytes =>
            {
                var success = EventConverter.TryParseActorId(bytes.Value, out var actorId);
                if (!success)
                {
                    if (this.Logger.IsEnabled(LogLevel.Error))
                    {
                        this.Logger.LogError($"{nameof(EventConverter.TryParseActorId)} failed");
                    }
                }

                return(success, actorId, bytes);
            }).Where(o => o.success).GroupBy(o => o.actorId);

            return(Task.WhenAll(groups.Select(async kv =>
            {
                var items = kv.Select(item => item.bytes.Value).ToList();
                switch (kv.Key)
                {
                case long id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                case string id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                case Guid id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                default: break;
                }
                foreach (var(_, _, bytes) in kv)
                {
                    bytes.Success = true;
                }
            })));
        }
Example #2
0
        public void ConvertToBytes(object id)
        {
            if (id is string strId && string.IsNullOrEmpty(strId))
            {
                id = Guid.NewGuid();
            }

            var eventName  = "TestName";
            var baseBytes  = Encoding.UTF8.GetBytes(eventName);
            var eventBytes = Encoding.UTF8.GetBytes(eventName);
            var eventUnit  = new EventTransUnit(eventName, id, baseBytes, eventBytes);

            using var array = eventUnit.ConvertToBytes();
            var bytes     = array.ToArray();
            var tryResult = EventConverter.TryParse(bytes, out var bitUnit);

            Assert.True(tryResult);
            Assert.Equal(id, bitUnit.ActorId);
            Assert.Equal(eventName, bitUnit.EventName);
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.MetaBytes));
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.EventBytes));
            tryResult = EventConverter.TryParseWithNoId(bytes, out bitUnit);
            Assert.True(tryResult);
            Assert.Null(bitUnit.ActorId);
            Assert.Equal(eventName, bitUnit.EventName);
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.MetaBytes));
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.EventBytes));
            tryResult = EventConverter.TryParseActorId(bytes, out var actorId);
            Assert.True(tryResult);
            Assert.Equal(id, actorId);
        }
Example #3
0
        public async Task EventHandler(Type observerType, BytesBox bytes)
        {
            if (EventConverter.TryParseActorId(bytes.Value, out var actorId))
            {
                switch (actorId)
                {
                case long id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                case string id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                case Guid id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                default: break;
                }
                bytes.Success = true;
            }
            else
            {
                if (this.Logger.IsEnabled(LogLevel.Error))
                {
                    this.Logger.LogError($"{nameof(EventConverter.TryParseActorId)} failed");
                }
            }
        }