示例#1
0
        public async Task Should_write_and_read_events_to_backup(BackupVersion version)
        {
            var stream = new MemoryStream();

            var random      = new Random();
            var randomGuids = new List <Guid>();

            for (var i = 0; i < 100; i++)
            {
                randomGuids.Add(Guid.NewGuid());
            }

            Guid RandomGuid()
            {
                return(randomGuids[random.Next(randomGuids.Count)]);
            }

            var sourceEvents = new List <(string Stream, Envelope <IEvent> Event)>();

            for (var i = 0; i < 200; i++)
            {
                var @event = new MyEvent
                {
                    GuidNamed = NamedId.Of(RandomGuid(), $"name{i}"),
                    GuidRaw   = RandomGuid(),
                    Values    = new Dictionary <Guid, string>
                    {
                        [RandomGuid()] = "Key"
                    }
                };

                var envelope = Envelope.Create <IEvent>(@event);

                envelope.Headers.Add(RandomGuid().ToString(), i);
                envelope.Headers.Add("Id", RandomGuid().ToString());
                envelope.Headers.Add("Index", i);

                sourceEvents.Add(($"My-{RandomGuid()}", envelope));
            }

            using (var writer = new BackupWriter(serializer, stream, true, version))
            {
                foreach (var(_, envelope) in sourceEvents)
                {
                    var eventData   = formatter.ToEventData(envelope, Guid.NewGuid(), true);
                    var eventStored = new StoredEvent("S", "1", 2, eventData);

                    var index = int.Parse(envelope.Headers["Index"].ToString());

                    if (index % 17 == 0)
                    {
                        await writer.WriteBlobAsync(index.ToString(), innerStream =>
                        {
                            innerStream.WriteByte((byte)index);

                            return(TaskHelper.Done);
                        });
                    }
                    else if (index % 37 == 0)
                    {
                        await writer.WriteJsonAsync(index.ToString(), $"JSON_{index}");
                    }

                    writer.WriteEvent(eventStored);
                }
            }

            stream.Position = 0;

            var targetEvents = new List <(string Stream, Envelope <IEvent> Event)>();

            using (var reader = new BackupReader(serializer, stream))
            {
                await reader.ReadEventsAsync(streamNameResolver, formatter, async @event =>
                {
                    var index = int.Parse(@event.Event.Headers["Index"].ToString());

                    if (index % 17 == 0)
                    {
                        await reader.ReadBlobAsync(index.ToString(), innerStream =>
                        {
                            var byteRead = innerStream.ReadByte();

                            Assert.Equal((byte)index, byteRead);

                            return(TaskHelper.Done);
                        });
                    }
                    else if (index % 37 == 0)
                    {
                        var json = await reader.ReadJsonAttachmentAsync <string>(index.ToString());

                        Assert.Equal($"JSON_{index}", json);
                    }

                    targetEvents.Add(@event);
                });

                void CompareGuid(Guid source, Guid target)
                {
                    Assert.Equal(source, reader.OldGuid(target));
                    Assert.NotEqual(source, target);
                }

                for (var i = 0; i < targetEvents.Count; i++)
                {
                    var target = targetEvents[i].Event.To <MyEvent>();

                    var source = sourceEvents[i].Event.To <MyEvent>();

                    CompareGuid(source.Payload.Values.First().Key, target.Payload.Values.First().Key);
                    CompareGuid(source.Payload.GuidRaw, target.Payload.GuidRaw);
                    CompareGuid(source.Payload.GuidNamed.Id, target.Payload.GuidNamed.Id);
                    CompareGuid(source.Headers.GetGuid("Id"), target.Headers.GetGuid("Id"));

                    Assert.Equal(Guid.Empty, target.Payload.GuidEmpty);
                }
            }
        }
示例#2
0
        public async Task Should_write_and_read_events()
        {
            var stream = new MemoryStream();

            var sourceEvents = new List <StoredEvent>();

            using (var writer = new BackupWriter(stream, true))
            {
                for (var i = 0; i < 1000; i++)
                {
                    var eventData = new EventData {
                        Type = i.ToString(), Metadata = i, Payload = i
                    };
                    var eventStored = new StoredEvent("S", "1", 2, eventData);

                    if (i % 17 == 0)
                    {
                        await writer.WriteBlobAsync(eventData.Type, innerStream =>
                        {
                            innerStream.WriteByte((byte)i);

                            return(TaskHelper.Done);
                        });
                    }
                    else if (i % 37 == 0)
                    {
                        await writer.WriteJsonAsync(eventData.Type, $"JSON_{i}");
                    }

                    writer.WriteEvent(eventStored);

                    sourceEvents.Add(eventStored);
                }
            }

            stream.Position = 0;

            var readEvents = new List <StoredEvent>();

            using (var reader = new BackupReader(stream))
            {
                await reader.ReadEventsAsync(streamNameResolver, async @event =>
                {
                    var i = int.Parse(@event.Data.Type);

                    if (i % 17 == 0)
                    {
                        await reader.ReadBlobAsync(@event.Data.Type, innerStream =>
                        {
                            var b = innerStream.ReadByte();

                            Assert.Equal((byte)i, b);

                            return(TaskHelper.Done);
                        });
                    }
                    else if (i % 37 == 0)
                    {
                        var j = await reader.ReadJsonAttachmentAsync(@event.Data.Type);

                        Assert.Equal($"JSON_{i}", j.ToString());
                    }

                    readEvents.Add(@event);
                });
            }

            var sourceEventsWithNewStreamName =
                sourceEvents.Select(x =>
                                    new StoredEvent(streamNameResolver.WithNewId(x.StreamName, null),
                                                    x.EventPosition,
                                                    x.EventStreamNumber,
                                                    x.Data)).ToList();

            readEvents.Should().BeEquivalentTo(sourceEventsWithNewStreamName);
        }