コード例 #1
0
        public async Task Messages_sent_from_one_bus_should_be_received_by_the_other()
        {
            using var messagebus1 = new TinyMessageBus("Example");
            using var messagebus2 = new TinyMessageBus("Example");

            var received = "nope";

            messagebus2.MessageReceived += (sender, e) => received = Encoding.UTF8.GetString(e.Message);

            await messagebus1.PublishAsync(Encoding.UTF8.GetBytes("lorem"));

            await messagebus2.PublishAsync(Encoding.UTF8.GetBytes("ipsum"));

            await messagebus1.PublishAsync(Encoding.UTF8.GetBytes("yes"));

            await messagebus2.ReadAsync();

            received.ShouldBe("yes");
        }
コード例 #2
0
        public async Task All_primitives_should_be_configurable()
        {
            var name           = "Example";
            var maxReaderCount = TinyReadWriteLock.DefaultMaxReaderCount;
            var maxFileSize    = TinyMemoryMappedFile.DefaultMaxFileSize;
            var waitTimeout    = TinyReadWriteLock.DefaultWaitTimeout;

            // Create underlying primitives first so they can be configured
            using var lockMutex        = TinyReadWriteLock.CreateMutex(name);
            using var lockSemaphore    = TinyReadWriteLock.CreateSemaphore(name, maxReaderCount);
            using var memoryMappedFile = TinyMemoryMappedFile.CreateOrOpenMemoryMappedFile(name, maxFileSize);
            using var eventWaitHandle  = TinyMemoryMappedFile.CreateEventWaitHandle(name);

            // Create the actual message bus
            using var tinyReadWriteLock    = new TinyReadWriteLock(lockMutex, lockSemaphore, maxReaderCount, waitTimeout);
            using var tinyMemoryMappedFile = new TinyMemoryMappedFile(memoryMappedFile, eventWaitHandle, maxFileSize, tinyReadWriteLock, disposeLock: true);

            using var messageBus = new TinyMessageBus(tinyMemoryMappedFile, disposeFile: true);
            await messageBus.PublishAsync(Encoding.UTF8.GetBytes("lorem"));

            await messageBus.PublishAsync(Encoding.UTF8.GetBytes("ipsum"));
        }