|
| 1 | +package zio.raft.sessionstatemachine |
| 2 | + |
| 3 | +import zio.test.* |
| 4 | +import scodec.Codec |
| 5 | +import scodec.codecs.* |
| 6 | +import zio.raft.sessionstatemachine.Codecs.given |
| 7 | +import zio.raft.protocol.* |
| 8 | +import java.time.Instant |
| 9 | +import zio.raft.Command |
| 10 | + |
| 11 | +object CodecsSpec extends ZIOSpecDefault: |
| 12 | + def spec = |
| 13 | + suiteAll("Session State Machine Codecs") { |
| 14 | + test("sessionMetadata round-trip") { |
| 15 | + val sm = SessionMetadata(Map("a" -> "b"), Instant.ofEpochMilli(1234L)) |
| 16 | + val bits = summon[Codec[SessionMetadata]].encode(sm).require |
| 17 | + val decoded = summon[Codec[SessionMetadata]].decode(bits).require.value |
| 18 | + assertTrue(decoded == sm) |
| 19 | + } |
| 20 | + |
| 21 | + test("requestId round-trip") { |
| 22 | + val r = RequestId(42L) |
| 23 | + val bits = Codecs.requestIdCodec.encode(r).require |
| 24 | + val decoded = Codecs.requestIdCodec.decode(bits).require.value |
| 25 | + assertTrue(decoded == r) |
| 26 | + } |
| 27 | + |
| 28 | + test("pendingServerRequest round-trip") { |
| 29 | + given Codec[String] = utf8_32 |
| 30 | + val psr = PendingServerRequest("payload", Instant.ofEpochMilli(999L)) |
| 31 | + val bits = summon[Codec[PendingServerRequest[String]]].encode(psr).require |
| 32 | + val decoded = summon[Codec[PendingServerRequest[String]]].decode(bits).require.value |
| 33 | + assertTrue(decoded == psr) |
| 34 | + } |
| 35 | + |
| 36 | + suiteAll("sessionCommand discriminated union round-trips") { |
| 37 | + test("ClientRequest") { |
| 38 | + // define a concrete UC that extends zio.raft.Command and provide a codec for it |
| 39 | + case object DummyCmd extends Command: |
| 40 | + type Response = Unit |
| 41 | + given Codec[DummyCmd.type] = provide(DummyCmd) |
| 42 | + given Codec[Unit] = provide(()) |
| 43 | + val cmd: SessionCommand[DummyCmd.type, Unit] = SessionCommand.ClientRequest[DummyCmd.type, Unit]( |
| 44 | + createdAt = Instant.EPOCH, |
| 45 | + sessionId = SessionId.fromString("s-1"), |
| 46 | + requestId = RequestId(1L), |
| 47 | + lowestPendingRequestId = RequestId(0L), |
| 48 | + command = DummyCmd |
| 49 | + ) |
| 50 | + val codec = summon[Codec[SessionCommand[DummyCmd.type, Unit]]] |
| 51 | + val bits = codec.encode(cmd).require |
| 52 | + val decoded = codec.decode(bits).require.value |
| 53 | + assertTrue(decoded == cmd) |
| 54 | + } |
| 55 | + |
| 56 | + test("ServerRequestAck") { |
| 57 | + // reuse Dummy UC type param for the union codec |
| 58 | + case object DummyCmd extends Command: |
| 59 | + type Response = Unit |
| 60 | + given Codec[DummyCmd.type] = provide(DummyCmd) |
| 61 | + given Codec[Unit] = provide(()) |
| 62 | + val cmd = SessionCommand.ServerRequestAck[Unit]( |
| 63 | + createdAt = Instant.EPOCH, |
| 64 | + sessionId = SessionId.fromString("s-2"), |
| 65 | + requestId = RequestId(2L) |
| 66 | + ) |
| 67 | + val codec = summon[Codec[SessionCommand[DummyCmd.type, Unit]]] |
| 68 | + val bits = codec.encode(cmd).require |
| 69 | + val decoded = codec.decode(bits).require.value |
| 70 | + assertTrue(decoded == cmd) |
| 71 | + } |
| 72 | + |
| 73 | + test("CreateSession") { |
| 74 | + case object DummyCmd extends Command: |
| 75 | + type Response = Unit |
| 76 | + given Codec[DummyCmd.type] = provide(DummyCmd) |
| 77 | + given Codec[Unit] = provide(()) |
| 78 | + val cmd = SessionCommand.CreateSession[Unit]( |
| 79 | + createdAt = Instant.EPOCH, |
| 80 | + sessionId = SessionId.fromString("s-3"), |
| 81 | + capabilities = Map("k" -> "v") |
| 82 | + ) |
| 83 | + val codec = summon[Codec[SessionCommand[DummyCmd.type, Unit]]] |
| 84 | + val bits = codec.encode(cmd).require |
| 85 | + val decoded = codec.decode(bits).require.value |
| 86 | + assertTrue(decoded == cmd) |
| 87 | + } |
| 88 | + |
| 89 | + test("SessionExpired") { |
| 90 | + case object DummyCmd extends Command: |
| 91 | + type Response = Unit |
| 92 | + given Codec[DummyCmd.type] = provide(DummyCmd) |
| 93 | + given Codec[Unit] = provide(()) |
| 94 | + val cmd = SessionCommand.SessionExpired[Unit]( |
| 95 | + createdAt = Instant.EPOCH, |
| 96 | + sessionId = SessionId.fromString("s-4") |
| 97 | + ) |
| 98 | + val codec = summon[Codec[SessionCommand[DummyCmd.type, Unit]]] |
| 99 | + val bits = codec.encode(cmd).require |
| 100 | + val decoded = codec.decode(bits).require.value |
| 101 | + assertTrue(decoded == cmd) |
| 102 | + } |
| 103 | + |
| 104 | + test("GetRequestsForRetry") { |
| 105 | + case object DummyCmd extends Command: |
| 106 | + type Response = Unit |
| 107 | + given Codec[DummyCmd.type] = provide(DummyCmd) |
| 108 | + given Codec[Unit] = provide(()) |
| 109 | + val cmd = SessionCommand.GetRequestsForRetry[Unit]( |
| 110 | + createdAt = Instant.EPOCH, |
| 111 | + lastSentBefore = Instant.ofEpochMilli(500L) |
| 112 | + ) |
| 113 | + val codec = summon[Codec[SessionCommand[DummyCmd.type, Unit]]] |
| 114 | + val bits = codec.encode(cmd).require |
| 115 | + val decoded = codec.decode(bits).require.value |
| 116 | + assertTrue(decoded == cmd) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +end CodecsSpec |
0 commit comments