Example #1
0
 public void Write_with_more_data_than_size_limit_throws()
 {
     using (var file = new TinyMemoryMappedFile("Test", 4))
     {
         Assert.Throws <ArgumentOutOfRangeException>(() => file.Write(new byte[] { 1, 2, 3, 4, 5 }));
     }
 }
Example #2
0
        public void GetFileSize_returns_expected_size(string message)
        {
            using var file = new TinyMemoryMappedFile("Test");

            var data = Encoding.UTF8.GetBytes(message);

            file.Write(data);

            Assert.Equal(message.Length, file.GetFileSize());
        }
Example #3
0
        public void Write_then_read_returns_what_was_written(string message)
        {
            using var file = new TinyMemoryMappedFile("Test");

            var data = Encoding.UTF8.GetBytes(message);

            file.Write(data);

            Assert.Equal(data, file.Read());
        }
Example #4
0
        public void Secondary_instance_keeps_file_alive()
        {
            using (var file2 = new TinyMemoryMappedFile("Test"))
            {
                using (var file1 = new TinyMemoryMappedFile("Test"))
                {
                    file1.Write(new byte[] { 1, 2, 3, 4, 5 });
                }

                Assert.Equal(5, file2.GetFileSize());
            }
        }
Example #5
0
        public void Dispose_destroys_file()
        {
            using (var file = new TinyMemoryMappedFile("Test"))
            {
                file.Write(new byte[] { 1, 2, 3, 4, 5 });
            }

            using (var file = new TinyMemoryMappedFile("Test"))
            {
                Assert.Equal(0, file.GetFileSize());
            }
        }
Example #6
0
        public void Dispose_destroys_file()
        {
            using (var file = new TinyMemoryMappedFile("Test"))
            {
                file.Write(new byte[] { 1, 2, 3, 4, 5 });
            }

            using (var file = new TinyMemoryMappedFile("Test"))
            {
                file.GetFileSize().ShouldBe(0);
            }
        }
Example #7
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"));
        }