|
| 1 | +using Servus.Akka.Transport; |
| 2 | +using Servus.Akka.Transport.Quic; |
| 3 | + |
| 4 | +namespace Servus.Akka.Tests.Transport.Quic; |
| 5 | + |
| 6 | +[Collection("TransportBuffer")] |
| 7 | +public sealed class QuicStreamStateSpec |
| 8 | +{ |
| 9 | + [Fact(Timeout = 5000)] |
| 10 | + public void New_state_should_be_Opening() |
| 11 | + { |
| 12 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 13 | + Assert.Equal(StreamPhase.Opening, state.Phase); |
| 14 | + Assert.False(state.HasHandle); |
| 15 | + } |
| 16 | + |
| 17 | + [Fact(Timeout = 5000)] |
| 18 | + public void Write_in_Opening_should_buffer() |
| 19 | + { |
| 20 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 21 | + var buf = TransportBuffer.Rent(2); |
| 22 | + buf.FullMemory.Span[0] = 0x01; |
| 23 | + buf.FullMemory.Span[1] = 0x02; |
| 24 | + buf.Length = 2; |
| 25 | + |
| 26 | + state.Write(buf); |
| 27 | + |
| 28 | + Assert.Equal(StreamPhase.Opening, state.Phase); |
| 29 | + Assert.Equal(1, state.PendingWriteCount); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact(Timeout = 5000)] |
| 33 | + public void CompleteWrites_in_Opening_should_defer() |
| 34 | + { |
| 35 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 36 | + state.CompleteWrites(); |
| 37 | + |
| 38 | + Assert.Equal(StreamPhase.Opening, state.Phase); |
| 39 | + Assert.True(state.IsCompleteWritesDeferred); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact(Timeout = 5000)] |
| 43 | + public void AttachHandle_should_transition_to_Active() |
| 44 | + { |
| 45 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 46 | + var handle = new StreamHandle(new MemoryStream()); |
| 47 | + |
| 48 | + state.AttachHandle(handle); |
| 49 | + |
| 50 | + Assert.Equal(StreamPhase.Active, state.Phase); |
| 51 | + Assert.True(state.HasHandle); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact(Timeout = 5000)] |
| 55 | + public void AttachHandle_should_flush_pending_writes() |
| 56 | + { |
| 57 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 58 | + var buf = TransportBuffer.Rent(2); |
| 59 | + buf.FullMemory.Span[0] = 0x01; |
| 60 | + buf.FullMemory.Span[1] = 0x02; |
| 61 | + buf.Length = 2; |
| 62 | + state.Write(buf); |
| 63 | + |
| 64 | + var handle = new StreamHandle(new MemoryStream()); |
| 65 | + state.AttachHandle(handle); |
| 66 | + |
| 67 | + Assert.Equal(0, state.PendingWriteCount); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact(Timeout = 5000)] |
| 71 | + public void AttachHandle_with_deferred_CompleteWrites_should_transition_to_HalfClosedWrite() |
| 72 | + { |
| 73 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 74 | + state.CompleteWrites(); |
| 75 | + |
| 76 | + state.AttachHandle(new StreamHandle(new MemoryStream())); |
| 77 | + |
| 78 | + Assert.Equal(StreamPhase.HalfClosedWrite, state.Phase); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact(Timeout = 5000)] |
| 82 | + public void CompleteWrites_in_Active_should_transition_to_HalfClosedWrite() |
| 83 | + { |
| 84 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 85 | + state.AttachHandle(new StreamHandle(new MemoryStream())); |
| 86 | + |
| 87 | + state.CompleteWrites(); |
| 88 | + |
| 89 | + Assert.Equal(StreamPhase.HalfClosedWrite, state.Phase); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact(Timeout = 5000)] |
| 93 | + public void OnReadCompleted_in_HalfClosedWrite_should_transition_to_Closed() |
| 94 | + { |
| 95 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 96 | + state.AttachHandle(new StreamHandle(new MemoryStream())); |
| 97 | + state.CompleteWrites(); |
| 98 | + |
| 99 | + state.OnReadCompleted(); |
| 100 | + |
| 101 | + Assert.Equal(StreamPhase.Closed, state.Phase); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact(Timeout = 5000)] |
| 105 | + public void OnReadCompleted_in_Active_should_transition_to_HalfClosedRead() |
| 106 | + { |
| 107 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 108 | + state.AttachHandle(new StreamHandle(new MemoryStream())); |
| 109 | + |
| 110 | + state.OnReadCompleted(); |
| 111 | + |
| 112 | + Assert.Equal(StreamPhase.HalfClosedRead, state.Phase); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact(Timeout = 5000)] |
| 116 | + public void Abort_should_transition_to_Closed() |
| 117 | + { |
| 118 | + var state = new QuicStreamState(StreamDirection.Bidirectional); |
| 119 | + state.AttachHandle(new StreamHandle(new MemoryStream())); |
| 120 | + |
| 121 | + state.Abort(0); |
| 122 | + |
| 123 | + Assert.Equal(StreamPhase.Closed, state.Phase); |
| 124 | + } |
| 125 | +} |
0 commit comments