2023-07-26 00:42:12 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-07-28 11:59:01 -06:00
|
|
|
"time"
|
2023-07-28 15:07:38 -06:00
|
|
|
"crypto/rand"
|
2023-08-06 12:47:47 -06:00
|
|
|
"crypto/ed25519"
|
2023-10-10 11:23:44 -06:00
|
|
|
"slices"
|
2023-07-26 00:42:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeDB(t *testing.T) {
|
2023-11-06 00:50:29 -07:00
|
|
|
ctx := logTestContext(t, []string{"signal", "serialize", "node", "db", "listener"})
|
2023-08-31 19:50:32 -06:00
|
|
|
node_type := NewNodeType("test")
|
2023-07-27 01:30:32 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType})
|
2023-07-26 00:42:12 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 01:30:32 -06:00
|
|
|
|
2023-09-02 17:30:52 -06:00
|
|
|
node_listener := NewListenerExt(10)
|
|
|
|
node, err := NewNode(ctx, nil, node_type, 10, nil, NewGroupExt(nil), NewLockableExt(nil), node_listener)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForSignal(node_listener.Chan, 10*time.Millisecond, func(sig *StatusSignal) bool {
|
2023-11-05 21:18:14 -07:00
|
|
|
gql_changes, has_gql := sig.Changes[GQLExtType]
|
|
|
|
if has_gql == true {
|
|
|
|
return slices.Contains(gql_changes, "state") && sig.Source == node.ID
|
|
|
|
}
|
|
|
|
return false
|
2023-09-02 17:30:52 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
msgs := Messages{}
|
2023-10-14 15:05:23 -06:00
|
|
|
msgs = msgs.Add(ctx, node.ID, node, nil, NewStopSignal())
|
2023-09-02 17:30:52 -06:00
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
_, err = WaitForSignal(node_listener.Chan, 10*time.Millisecond, func(sig *StoppedSignal) bool {
|
|
|
|
return sig.Source == node.ID
|
2023-09-02 17:30:52 -06:00
|
|
|
})
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-26 00:42:12 -06:00
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
ctx.nodeMap = map[NodeID]*Node{}
|
|
|
|
_, err = ctx.getNode(node.ID)
|
2023-07-26 00:42:12 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
func TestNodeRead(t *testing.T) {
|
2023-08-28 14:52:28 -06:00
|
|
|
ctx := logTestContext(t, []string{"test"})
|
2023-08-31 19:50:32 -06:00
|
|
|
node_type := NewNodeType("TEST")
|
2023-09-12 20:30:18 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType})
|
2023-07-28 11:59:01 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-06 12:47:47 -06:00
|
|
|
n1_pub, n1_key, err := ed25519.GenerateKey(rand.Reader)
|
2023-07-28 15:07:38 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-06 12:47:47 -06:00
|
|
|
n2_pub, n2_key, err := ed25519.GenerateKey(rand.Reader)
|
2023-07-28 15:07:38 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-06 12:47:47 -06:00
|
|
|
n1_id := KeyID(n1_pub)
|
|
|
|
n2_id := KeyID(n2_pub)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "N1: %s", n1_id)
|
|
|
|
ctx.Log.Logf("test", "N2: %s", n2_id)
|
|
|
|
|
2023-08-28 14:52:28 -06:00
|
|
|
n1_policy := NewPerNodePolicy(map[NodeID]Tree{
|
2023-08-31 19:50:32 -06:00
|
|
|
n2_id: {
|
2023-09-05 00:08:09 -06:00
|
|
|
SerializedType(ReadSignalType): nil,
|
2023-08-28 14:52:28 -06:00
|
|
|
},
|
|
|
|
})
|
2023-08-11 16:00:36 -06:00
|
|
|
|
2023-07-28 11:59:01 -06:00
|
|
|
n2_listener := NewListenerExt(10)
|
2023-08-31 22:31:29 -06:00
|
|
|
n2, err := NewNode(ctx, n2_key, node_type, 10, nil, NewGroupExt(nil), n2_listener)
|
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-09-20 19:14:28 -06:00
|
|
|
n1, err := NewNode(ctx, n1_key, node_type, 10, []Policy{n1_policy}, NewGroupExt(nil))
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
read_sig := NewReadSignal(map[ExtType][]string{
|
2023-08-31 19:50:32 -06:00
|
|
|
GroupExtType: {"members"},
|
2023-08-01 20:55:15 -06:00
|
|
|
})
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-10-14 15:05:23 -06:00
|
|
|
msgs = msgs.Add(ctx, n1.ID, n2, nil, read_sig)
|
2023-08-08 14:00:17 -06:00
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-08-31 19:50:32 -06:00
|
|
|
res, err := WaitForSignal(n2_listener.Chan, 10*time.Millisecond, func(sig *ReadResultSignal) bool {
|
2023-07-28 19:32:27 -06:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx.Log.Logf("test", "READ_RESULT: %+v", res)
|
|
|
|
}
|