public async Task save_write_throws() { var stream = new Mock <Stream>(); stream.Setup(s => s.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Throws(new Exception("Stream.Write")); stream.Setup(s => s.CanWrite).Returns(true); var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenWriteAsync("test")), stream.Object); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("0"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Throws(new Exception("Projection.TrySaveAsync")); var reified = Make(projection.Object, cache.Object); try { await reified.TrySaveAsync(CancellationToken.None); Assert.Fail("Exception expected."); } catch (Exception e) { Assert.AreEqual("Stream.Write", e.Message); } }
public override async Task save_auto_inconsistent() { var ms = new MemoryStream(); var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenWriteAsync("test")), ms); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("0"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Throws(new Exception("Projection.TrySaveAsync")); projection.Setup(p => p.Apply(It.IsAny <uint>(), It.IsAny <int>(), It.IsAny <string>())) .Throws(new Exception("Boo.")); var reified = Make(projection.Object, cache.Object); Assert.Throws <AggregateException>(() => reified.Apply(1U, 13), "Boo."); // Sets 'inconsistent' await reified.TrySaveAsync(CancellationToken.None); CollectionAssert.AreEqual(new byte[] { // Cut short }, ms.ToArray()); }
public async Task save_reset_inconsistent() { var ms = new MemoryStream(); var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenWriteAsync("test")), ms); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("0"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns <Stream, string, CancellationToken>((s, state, c) => { var bytes = Encoding.UTF8.GetBytes((string)state); s.Write(bytes, 0, bytes.Length); return(Task.FromResult(true)); }); var reified = Make(projection.Object, cache.Object); reified.SetPossiblyInconsistent(); reified.Reset(); await reified.TrySaveAsync(CancellationToken.None); CollectionAssert.AreEqual(new byte[] { 0x00, 0x00, 0x00, 0x00, // Sequence number (first) 0x30, // Contents "0" 0x00, 0x00, 0x00, 0x00 // Sequence number (last) }, ms.ToArray()); }
public async Task save_throws() { var ms = new MemoryStream(); var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenWriteAsync("test")), ms); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("0"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Throws(new Exception("Projection.TrySaveAsync")); var reified = Make(projection.Object, cache.Object); try { await reified.TrySaveAsync(CancellationToken.None); Assert.Fail("Exception expected."); } catch (Exception e) { Assert.AreEqual("Projection.TrySaveAsync", e.Message); } CollectionAssert.AreEqual(new byte[] { 0x00, 0x00, 0x00, 0x00, // Sequence number (first) // Cut short }, ms.ToArray()); }
public async Task load_state_bad_name() { var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("test")), new MemoryStream(new byte[] { 0x02, 0x00, 0x00, 0x00, // Current position (beginning) 0x30, 0x30, 0x30, 0x30, // Event data "0000" 0x02, 0x00, 0x00, 0x00 // Current position (end) })); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("other")), new MemoryStream(new byte[0])); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("I"); projection.Setup(p => p.FullName).Returns("other"); projection.Setup(p => p.State).Returns(typeof(string)); ReturnsExtensions.ReturnsAsync(projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())), "bad"); var reified = Make(projection.Object, cache.Object); await reified.TryLoadAsync(CancellationToken.None); Assert.AreEqual((uint)0U, (uint)reified.Sequence); Assert.AreEqual("I", reified.Current); }
public async Task load_state() { var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("test")), new MemoryStream(new byte[] { 0x02, 0x00, 0x00, 0x00, // Current position (beginning) 0x30, 0x30, 0x30, 0x30, // Event data "0000" 0x02, 0x00, 0x00, 0x00 // Current position (end) })); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("I"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns <Stream, CancellationToken>((s, c) => { var bytes = new byte[4]; s.Read(bytes, 0, 4); return(Task.FromResult(Encoding.UTF8.GetString(bytes))); }); var reified = Make(projection.Object, cache.Object); await reified.TryLoadAsync(CancellationToken.None); Assert.AreEqual((uint)2U, (uint)reified.Sequence); Assert.AreEqual("0000", reified.Current); }
public async Task load_state_bad_name() { var cache = new Testing.InMemoryCache { { "test", new byte[] { 0x02, 0x00, 0x00, 0x00, // Current position (beginning) 0x30, 0x30, 0x30, 0x30, // Event data "0000" 0x02, 0x00, 0x00, 0x00 // Current position (end) } }, { "other", new byte[0] } }; var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("I"); projection.Setup(p => p.FullName).Returns("other"); projection.Setup(p => p.State).Returns(typeof(string)); ReturnsExtensions.ReturnsAsync(projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())), "bad"); var reified = Make(projection.Object, cache); await reified.TryLoadAsync(CancellationToken.None); Assert.Equal(0U, reified.Sequence); Assert.Equal("I", reified.Current); }
public void GetFailure() { ReturnsExtensions.ReturnsAsync(_firebaseRequestManagerMock.Setup( firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", (object)null)), _failureResponse); Assert.Throws <FirebaseException>(() => _firebaseClient.Get("todos")); }
public void DeleteAsyncFailure() { ReturnsExtensions.ReturnsAsync(_firebaseRequestManagerMock.Setup( firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Delete, "todos", (object)null)), _failureResponse); Assert.Throws <FirebaseException>(async() => await _firebaseClient.DeleteAsync("todos")); }
public void UpdateAsyncFailure() { ReturnsExtensions.ReturnsAsync(_firebaseRequestManagerMock.Setup( firebaseRequestManager => firebaseRequestManager.RequestAsync(RequestManager.Patch, "todos", _expected)), _failureResponse); Assert.Throws <FirebaseException>(async() => await _firebaseClient.UpdateAsync("todos", _expected)); }
public void PushAsyncFailure() { ReturnsExtensions.ReturnsAsync(_firebaseRequestManagerMock.Setup( firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Post, "todos", _expected)), _failureResponse); Assert.Throws <FirebaseException>(async() => await _firebaseClient.PushAsync("todos", _expected)); }
public async Task multi_apply_separate() { var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("string")), new MemoryStream(new byte[] { 0x02, 0x00, 0x00, 0x00, // Current position (beginning) 0x30, 0x30, 0x30, 0x30, // Event data "0000" 0x02, 0x00, 0x00, 0x00 // Current position (end) })); var str = MockString(); str.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns <Stream, CancellationToken>((s, c) => { var bytes = new byte[4]; s.Read(bytes, 0, 4); return(Task.FromResult(Encoding.UTF8.GetString(bytes))); }); var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[] { MockInteger().Object, str.Object }, cache.Object); await reified.TryLoadAsync(CancellationToken.None); reified.Apply(1U, 10); reified.Apply(4U, 14); Assert.AreEqual((uint)4U, (uint)reified.Sequence); Assert.AreEqual((int)24, (int)reified.Current.I.Value); Assert.AreEqual("0000(14:4)", reified.Current.S); }
public async Task load_state_sream_throws() { var stream = new Mock <Stream>(); stream.Setup(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Throws(new Exception("Stream.Read")); stream.Setup(s => s.CanRead).Returns(true); var cache = new Mock <IProjectionCacheProvider>(); ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("test")), stream.Object); var projection = new Mock <IProjection <int, string> >(); projection.Setup(p => p.Initial).Returns("I"); projection.Setup(p => p.FullName).Returns("test"); projection.Setup(p => p.State).Returns(typeof(string)); ReturnsExtensions.ReturnsAsync(projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())), "bad"); var reified = Make(projection.Object, cache.Object); try { await reified.TryLoadAsync(CancellationToken.None); Assert.Fail("Expected exception"); } catch (Exception e) { Assert.AreEqual("Stream.Read", e.Message); } Assert.AreEqual((uint)0U, (uint)reified.Sequence); Assert.AreEqual("I", reified.Current); }