public void WriteBytes(byte[] bytes) { if (_isOpen) { if (bytes.Length > _sharedBuf.Length) { Resize(bytes.Length); } _sharedBuf.Write(bytes); } }
public void Write_NullArray_ThrowsException() { var name = Guid.NewGuid().ToString(); using (var sma = new SharedArray <int>(name, 10)) { bool exceptionThrown = false; try { sma.Write(null); } catch (ArgumentNullException) { exceptionThrown = true; } Assert.IsTrue(exceptionThrown, "null buffer should result in ArgumentNullException"); } }
public void GetEnumerator_IterateItems_DataMatches() { var name = Guid.NewGuid().ToString(); Random r = new Random(); int bufSize = 1024; byte[] data = new byte[bufSize]; byte[] readBuf = new byte[bufSize]; using (var sma = new SharedArray <byte>(name, bufSize)) { sma.Write(data); int value = 0; foreach (var item in sma) { Assert.AreEqual(data[value], item); value++; } } }
public void AcquireWriteLock_ReadWrite_LocksCorrectly() { var name = Guid.NewGuid().ToString(); Random r = new Random(); int bufSize = 1024; byte[] data = new byte[bufSize]; byte[] readBuf = new byte[bufSize]; bool readIsFirst = false; bool readBlocked = false; int syncValue = 0; // Fill with random data r.NextBytes(data); using (var sma = new SharedArray <byte>(name, bufSize)) { // Acquire write lock early sma.AcquireWriteLock(); using (var smr = new SharedArray <byte>(name)) { var t1 = Task.Factory.StartNew(() => { if (System.Threading.Interlocked.Exchange(ref syncValue, 1) == 0) { readIsFirst = true; } // Should block until write lock is released smr.AcquireReadLock(); if (System.Threading.Interlocked.Exchange(ref syncValue, 3) == 4) { readBlocked = true; } smr.CopyTo(readBuf); smr.ReleaseReadLock(); }); System.Threading.Thread.Sleep(10); var t2 = Task.Factory.StartNew(() => { var val = System.Threading.Interlocked.Exchange(ref syncValue, 2); if (val == 0) { readIsFirst = false; } else if (val == 3) { readBlocked = false; } System.Threading.Thread.Sleep(10); sma.Write(data); System.Threading.Interlocked.Exchange(ref syncValue, 4); sma.ReleaseWriteLock(); }); Task.WaitAll(t1, t2); Assert.IsTrue(readIsFirst, "The read thread did not enter first."); Assert.IsTrue(readBlocked, "The read thread did not block."); // Check data was written before read for (var i = 0; i < readBuf.Length; i++) { Assert.AreEqual(data[i], readBuf[i]); } } } }