Plumb through client requests to UI

This commit is contained in:
Smaug123
2022-11-07 09:40:18 +00:00
parent e7b2f65596
commit 5df2910b7f
11 changed files with 413 additions and 86 deletions

View File

@@ -15,18 +15,24 @@ module TestInMemoryPersistentState =
s.CurrentLogIndex |> shouldEqual 0<LogIndex>
for i in -2 .. 10 do
s.GetLogEntry (i * 1<LogIndex>) |> shouldEqual None
match s.GetLogEntry (i * 1<LogIndex>) with
| Some _ -> failwith "should not have had a log entry"
| None -> ()
s.CurrentTerm |> shouldEqual 0<Term>
s.VotedFor |> shouldEqual None
s.GetLastLogEntry () |> shouldEqual None
match s.GetLastLogEntry () with
| Some _ -> failwith "should not have had a log entry"
| None -> ()
let ofList<'a> (xs : ('a * int<Term>) list) : InMemoryPersistentState<'a> =
let s = InMemoryPersistentState<'a> ()
for x, term in xs do
(s :> IPersistentState<_>).AppendToLog x term
(s :> IPersistentState<_>).AppendToLog
(LogEntry.ClientEntry (x, 1<ClientId>, 1<ClientSequence>, ignore))
term
s
@@ -38,26 +44,39 @@ module TestInMemoryPersistentState =
[<Test>]
let ``Nonzero truncation followed by Get succeeds`` () =
let property (truncate : int<LogIndex>) (xs : (int * int<Term>) list) : bool =
let property (truncate : int<LogIndex>) (xs : (byte * int<Term>) list) : bool =
let truncate = abs truncate + 1<LogIndex>
let uut = ofList xs
let oldLog = uut.GetLog ()
let oldLog =
uut.GetLog ()
|> List.map (fun (entry, term) -> SerialisedLogEntry.Make entry, term)
match (uut :> IPersistentState<_>).GetLogEntry truncate with
| None ->
(uut :> IPersistentState<_>).TruncateLog truncate
uut.GetLog () = oldLog
let newLog =
uut.GetLog ()
|> List.map (fun (entry, term) -> SerialisedLogEntry.Make entry, term)
newLog = oldLog
| Some (itemStored, entry) ->
(uut :> IPersistentState<_>).TruncateLog truncate
(uut :> IPersistentState<_>).GetLastLogEntry () = Some (
itemStored,
{
Index = truncate
Term = entry
}
)
&& isPrefix (uut.GetLog ()) oldLog
let newLog =
uut.GetLog ()
|> List.map (fun (entry, term) -> SerialisedLogEntry.Make entry, term)
let retrieved, logEntry =
Option.get ((uut :> IPersistentState<_>).GetLastLogEntry ())
logEntry = {
Index = truncate
Term = entry
}
&& (SerialisedLogEntry.Make retrieved = SerialisedLogEntry.Make itemStored)
&& isPrefix newLog oldLog
&& (uut :> IPersistentState<_>).CurrentLogIndex = truncate
Check.QuickThrowOnFailure property
@@ -69,13 +88,20 @@ module TestInMemoryPersistentState =
let uut = ofList xs
// It's not meaningful to take the 0th element
(uut :> IPersistentState<_>).GetLogEntry truncate |> shouldEqual None
match (uut :> IPersistentState<_>).GetLogEntry truncate with
| Some _ -> failwith "should not have had any elements"
| None -> ()
(uut :> IPersistentState<_>).TruncateLog truncate
uut.GetLog () |> shouldEqual []
match uut.GetLog () with
| [] -> ()
| _ -> failwith "should not have had log entries"
let uut = uut :> IPersistentState<_>
uut.GetLastLogEntry () = None && uut.CurrentLogIndex = 0<LogIndex>
match uut.GetLastLogEntry () with
| Some _ -> false
| None -> uut.CurrentLogIndex = 0<LogIndex>
Check.QuickThrowOnFailure property

View File

@@ -262,6 +262,20 @@ module TestInMemoryServer =
let clusterSize = 5
let cluster, network = InMemoryCluster.make<byte> clusterSize
let registeredSuccessfully = ref 0
let registerResponse (response : RegisterClientResponse) : unit =
response |> shouldEqual (RegisterClientResponse.Success 1<ClientId>)
Interlocked.Increment registeredSuccessfully |> ignore
let respondedSuccessfully = ref 0
let requestResponse (response : ClientResponse) : unit =
response
|> shouldEqual (ClientResponse.Success (1<ClientId>, 1<ClientSequence>))
Interlocked.Increment registeredSuccessfully |> ignore
let startupSequence =
[
NetworkAction.InactivityTimeout 1<ServerId>
@@ -287,7 +301,11 @@ module TestInMemoryServer =
NetworkAction.NetworkMessage (1<ServerId>, 7)
// Submit data to leader. This has the effect of heartbeating the other
// nodes, with a heartbeat that contains the new data.
NetworkAction.ClientRequest (1<ServerId>, byte 3)
NetworkAction.ClientRequest (1<ServerId>, ClientRequest.RegisterClient registerResponse)
NetworkAction.ClientRequest (
1<ServerId>,
ClientRequest.ClientRequest (1<ClientId>, 1<ClientSequence>, byte 3, requestResponse)
)
// Deliver the data messages.
NetworkAction.NetworkMessage (0<ServerId>, 2)
@@ -330,6 +348,9 @@ module TestInMemoryServer =
}
]
respondedSuccessfully.Value |> shouldEqual 1
registeredSuccessfully.Value |> shouldEqual 1
let freeze<'a> (cluster : Cluster<'a>) =
List.init
cluster.ClusterSize

View File

@@ -57,12 +57,14 @@ module ValidHistory =
yield NetworkAction.InactivityTimeout server
]
(*
let clientRequestGen =
gen {
let! element = elementGen
let! id = Gen.choose (0, clusterSize - 1)
return NetworkAction.ClientRequest (id * 1<ServerId>, element)
}
*)
let rec go (len : int) =
gen {
@@ -71,9 +73,11 @@ module ValidHistory =
else
let! smaller = go (len - 1)
let! next =
let! next = Gen.elements (permissibleNext ())
(*
clientRequestGen :: List.replicate 5 (Gen.elements (permissibleNext ()))
|> Gen.oneof
*)
NetworkAction.perform cluster network next
return next :: smaller