public static void MemoryStream_LengthTest() { using (PooledMemoryStream ms2 = new PooledMemoryStream()) { // [] Get the Length when position is at length ms2.SetLength(50); ms2.Position = 50; StreamWriter sw2 = new StreamWriter(ms2); for (char c = 'a'; c < 'f'; c++) { sw2.Write(c); } sw2.Flush(); Assert.Equal(55, ms2.Length); // Somewhere in the middle (set the length to be shorter.) ms2.SetLength(30); Assert.Equal(30, ms2.Length); Assert.Equal(30, ms2.Position); // Increase the length ms2.SetLength(100); Assert.Equal(100, ms2.Length); Assert.Equal(30, ms2.Position); } }
public static void MemoryStream_LengthTest_Negative() { using (PooledMemoryStream ms2 = new PooledMemoryStream()) { Assert.Throws <ArgumentOutOfRangeException>(() => ms2.SetLength(long.MaxValue)); Assert.Throws <ArgumentOutOfRangeException>(() => ms2.SetLength(-2)); } }
/// <summary> /// 发送无状态更改的消息到消息队列 /// </summary> /// <returns></returns> protected async ValueTask Publish(IMessage msg, string hashKey = null) { if (string.IsNullOrEmpty(hashKey)) { hashKey = GrainId.ToString(); } using (var ms = new PooledMemoryStream()) { Serializer.Serialize(ms, msg); var data = new W { TypeCode = msg.GetType().FullName, BinaryBytes = ms.ToArray() }; ms.Position = 0; ms.SetLength(0); Serializer.Serialize(ms, data); var mqServiceTask = GetMQService(); if (!mqServiceTask.IsCompleted) { await mqServiceTask; } var pubLishTask = mqServiceTask.Result.Publish(ms.ToArray(), hashKey); if (!pubLishTask.IsCompleted) { await pubLishTask; } } }
public void ShrinkFromTwoSegment() { PooledMemoryStream stream = s_manager.GetStream(); byte[] buffer = new byte[12]; new Random(42).NextBytes(buffer); stream.Write(buffer); stream.Write(buffer); Assert.Equal(32, stream.Capacity); Assert.Equal(24, stream.Length); Assert.Equal(24, stream.Position); stream.SetLength(12); Assert.Equal(16, stream.Capacity); Assert.Equal(12, stream.Length); Assert.Equal(12, stream.Position); stream.Seek(0, SeekOrigin.Begin); byte[] buffer2 = new byte[12]; Assert.Equal(12, stream.Read(buffer2)); Assert.True(buffer2.AsSpan(0, 12).SequenceEqual(buffer.AsSpan(0, 12))); }
public void ExpandOnSingleSegment() { PooledMemoryStream stream = s_manager.GetStream(); byte[] buffer = new byte[4]; new Random(42).NextBytes(buffer); stream.Write(buffer); Assert.Equal(16, stream.Capacity); Assert.Equal(4, stream.Length); Assert.Equal(4, stream.Position); stream.SetLength(8); Assert.Equal(16, stream.Capacity); Assert.Equal(8, stream.Length); Assert.Equal(4, stream.Position); stream.Seek(0, SeekOrigin.Begin); byte[] buffer2 = new byte[8]; Assert.Equal(8, stream.Read(buffer2)); Assert.True(buffer2.AsSpan(0, 4).SequenceEqual(buffer.AsSpan(0, 4))); Assert.True(buffer2.AsSpan(4, 4).SequenceEqual(s_emptyBytes.AsSpan(0, 4))); }
protected virtual async ValueTask <bool> RaiseEvent(IEventBase <K> @event, string uniqueId = null, string hashKey = null) { try { State.IncrementDoingVersion();//标记将要处理的Version @event.StateId = GrainId; @event.Version = State.Version + 1; @event.Timestamp = DateTime.UtcNow; var serializer = GetSerializer(); using (var ms = new PooledMemoryStream()) { serializer.Serialize(ms, @event); var bytes = ms.ToArray(); if (await(await GetEventStorage()).SaveAsync(@event, bytes, uniqueId)) { if (SupportAsync) { var data = new W { TypeCode = @event.TypeCode, BinaryBytes = bytes }; ms.Position = 0; ms.SetLength(0); serializer.Serialize(ms, data); //消息写入消息队列,以提供异步服务 Apply(State, @event); GetMQService().Publish(ms.ToArray(), string.IsNullOrEmpty(hashKey) ? GrainId.ToString() : hashKey); } else { Apply(State, @event); } State.UpdateVersion(@event);//更新处理完成的Version await SaveSnapshotAsync(); OnRaiseSuccess(@event, bytes); return(true); } else { State.DecrementDoingVersion();//还原doing Version } } } catch (Exception ex) { await RecoveryState();//还原状态 ExceptionDispatchInfo.Capture(ex).Throw(); } return(false); }
public void TestSetLength() { var data = new byte[] { 1, 2, 3, 4 }; using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared)) { stm.Write(data, 0, data.Length); stm.SetLength(128 * 1024); Assert.Equal(128 * 1024, stm.Length); var ar = stm.ToArray(); Assert.Equal(data, ar.Take(data.Length)); } }
protected async ValueTask <bool> RaiseEvent(IEventBase <K> @event, string uniqueId = null, string hashKey = null) { try { State.IncrementDoingVersion();//标记将要处理的Version @event.StateId = GrainId; @event.Version = State.Version + 1; @event.Timestamp = DateTime.UtcNow; var serializer = GetSerializer(); using (var ms = new PooledMemoryStream()) { serializer.Serialize(ms, @event); var bytes = ms.ToArray(); var result = await GetEventStorage().SaveAsync(@event, bytes, uniqueId); if (result) { EventHandle.Apply(State, @event); if (SupportAsync) { var data = new W { TypeCode = @event.TypeCode, BinaryBytes = bytes }; ms.Position = 0; ms.SetLength(0); serializer.Serialize(ms, data); //消息写入消息队列,以提供异步服务 await GetMQService().Publish(ms.ToArray(), string.IsNullOrEmpty(hashKey) ? GrainId.ToString() : hashKey); } State.UpdateVersion(@event);//更新处理完成的Version await SaveSnapshotAsync(); return(true); } else { State.DecrementDoingVersion();//还原doing Version } } } catch (Exception ex) { State.DecrementDoingVersion();//还原doing Version Logger.LogError(LogEventIds.EventRaiseError, ex, "Apply event {0} error, EventId={1}", @event.TypeCode, @event.Version); throw ex; } return(false); }
public void ExpandUsingSmallSourceOnEmptyInstance() { var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions() { MinimumSegmentSize = 16, MaximumSegmentSize = 16 }); PooledMemoryStream stream = manager.GetStream(); stream.SetLength(32); Assert.Equal(32, stream.Length); Assert.Equal(0, stream.Position); Assert.Equal(32, stream.Capacity); }
protected async ValueTask CommitTransaction() { if (transactionEventList.Count > 0) { var serializer = GetSerializer(); using (var ms = new PooledMemoryStream()) { foreach (var @event in transactionEventList) { serializer.Serialize(ms, @event.Evt); @event.Bytes = ms.ToArray(); ms.Position = 0; ms.SetLength(0); } } await(await GetEventStorage()).TransactionSaveAsync(transactionEventList); if (SupportAsync) { var mqService = GetMQService(); using (var ms = new PooledMemoryStream()) { foreach (var @event in transactionEventList) { var data = new W { TypeCode = @event.Evt.TypeCode, BinaryBytes = @event.Bytes }; serializer.Serialize(ms, data); mqService.Publish(ms.ToArray(), @event.HashKey); OnRaiseSuccess(@event.Evt, @event.Bytes); ms.Position = 0; ms.SetLength(0); } } } else { foreach (var evt in transactionEventList) { OnRaiseSuccess(evt.Evt, evt.Bytes); } } transactionEventList.Clear(); await SaveSnapshotAsync(); } transactionPending = false; }
public void ExpandUsingSmallSource() { var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions() { MinimumSegmentSize = 16, MaximumSegmentSize = 16 }); PooledMemoryStream stream = manager.GetStream(); stream.Write(new byte[10]); stream.SetLength(48); Assert.Equal(48, stream.Length); Assert.Equal(10, stream.Position); Assert.Equal(48, stream.Capacity); }
public void SetLengthOnEmptyInstance() { PooledMemoryStream stream = s_manager.GetStream(); stream.SetLength(10); Assert.Equal(10, stream.Length); Assert.Equal(16, stream.Capacity); Assert.Equal(0, stream.Position); byte[] buffer = new byte[11]; new Random(42).NextBytes(buffer); Assert.Equal(10, stream.Read(buffer, 0, 10)); Assert.True(buffer.AsSpan(0, 10).SequenceEqual(s_emptyBytes.AsSpan(0, 10))); stream.Seek(0, SeekOrigin.Begin); Assert.Equal(10, stream.Read(buffer, 0, 11)); }
/// <summary> /// 发送无状态更改的消息到消息队列 /// </summary> /// <returns></returns> public async Task Publish(IMessage msg, string hashKey = null) { if (string.IsNullOrEmpty(hashKey)) { hashKey = GrainId.ToString(); } var serializer = GetSerializer(); using (var ms = new PooledMemoryStream()) { serializer.Serialize(ms, msg); var data = new W { TypeCode = msg.TypeCode, BinaryBytes = ms.ToArray() }; ms.Position = 0; ms.SetLength(0); serializer.Serialize(ms, data); await GetMQService().Publish(ms.ToArray(), hashKey); } }
public async Task Go() { Data d = new Data(); var mqService = mQServiceContainer.GetService(this); using (var ms = new PooledMemoryStream()) { Serializer.Serialize(ms, d); var data = new MessageInfo() { TypeCode = d.GetType().FullName, BinaryBytes = ms.ToArray() }; ms.Position = 0; ms.SetLength(0); Serializer.Serialize(ms, data); for (int i = 0; i < 2; i++) { await mqService.Result.Publish(ms.ToArray(), Guid.NewGuid().ToString()); Console.WriteLine(i); } } }
protected async Task CommitTransaction() { if (transactionEventList.Count > 0) { using (var ms = new PooledMemoryStream()) { foreach (var @event in transactionEventList) { Serializer.Serialize(ms, @event.Evt); @event.Bytes = ms.ToArray(); ms.Position = 0; ms.SetLength(0); } } var eventStorageTask = GetEventStorage(); if (!eventStorageTask.IsCompleted) { await eventStorageTask; } await eventStorageTask.Result.TransactionSaveAsync(transactionEventList); if (SupportAsyncFollow) { var mqService = GetMQService(); if (!mqService.IsCompleted) { await mqService; } using (var ms = new PooledMemoryStream()) { foreach (var @event in transactionEventList) { var data = new W { TypeName = @event.Evt.GetType().FullName, Bytes = @event.Bytes }; Serializer.Serialize(ms, data); var publishTask = mqService.Result.Publish(ms.ToArray(), @event.HashKey); if (!publishTask.IsCompleted) { await publishTask; } OnRaiseSuccess(@event.Evt, @event.Bytes); ms.Position = 0; ms.SetLength(0); } } } else { foreach (var evt in transactionEventList) { OnRaiseSuccess(evt.Evt, evt.Bytes); } } transactionEventList.Clear(); var saveSnapshotTask = SaveSnapshotAsync(); if (!saveSnapshotTask.IsCompleted) { await saveSnapshotTask; } } transactionPending = false; }
protected virtual async Task <bool> RaiseEvent(IEventBase <K> @event, string uniqueId = null, string hashKey = null) { try { State.IncrementDoingVersion(GrainType);//标记将要处理的Version @event.StateId = GrainId; @event.Version = State.Version + 1; @event.Timestamp = DateTime.UtcNow; using (var ms = new PooledMemoryStream()) { Serializer.Serialize(ms, @event); var bytes = ms.ToArray(); var getEventStorageTask = GetEventStorage(); if (!getEventStorageTask.IsCompleted) { await getEventStorageTask; } if (await getEventStorageTask.Result.SaveAsync(@event, bytes, uniqueId)) { if (SupportAsyncFollow) { var data = new W { TypeName = @event.GetType().FullName, Bytes = bytes }; ms.Position = 0; ms.SetLength(0); Serializer.Serialize(ms, data); //消息写入消息队列,以提供异步服务 Apply(State, @event); var mqServiceTask = GetMQService(); if (!mqServiceTask.IsCompleted) { await mqServiceTask; } var publishTask = mqServiceTask.Result.Publish(ms.ToArray(), string.IsNullOrEmpty(hashKey) ? GrainId.ToString() : hashKey); if (!publishTask.IsCompleted) { await publishTask; } } else { Apply(State, @event); } State.UpdateVersion(@event, GrainType);//更新处理完成的Version var saveSnapshotTask = SaveSnapshotAsync(); if (!saveSnapshotTask.IsCompleted) { await saveSnapshotTask; } OnRaiseSuccess(@event, bytes); return(true); } else { State.DecrementDoingVersion();//还原doing Version } } } catch (Exception ex) { await RecoveryState();//还原状态 ExceptionDispatchInfo.Capture(ex).Throw(); } return(false); }