graphvent/node_test.go

75 lines
1.9 KiB
Go

package graphvent
import (
"testing"
"time"
"crypto/rand"
2023-08-06 12:47:47 -06:00
"crypto/ed25519"
"slices"
)
func TestNodeDB(t *testing.T) {
ctx := logTestContext(t, []string{"node", "db"})
node_listener := NewListenerExt(10)
2024-03-03 15:45:45 -07:00
node, err := NewNode(ctx, nil, "Base", 10, nil, NewGroupExt(nil), NewLockableExt(nil), node_listener)
fatalErr(t, err)
_, err = WaitForSignal(node_listener.Chan, 10*time.Millisecond, func(sig *StatusSignal) bool {
2024-03-03 15:45:45 -07:00
gql_changes, has_gql := sig.Changes[ExtTypeFor[GQLExt]()]
if has_gql == true {
return slices.Contains(gql_changes, "state") && sig.Source == node.ID
}
return false
})
err = ctx.Unload(node.ID)
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
ctx.nodeMap = map[NodeID]*Node{}
_, err = ctx.getNode(node.ID)
fatalErr(t, err)
}
func TestNodeRead(t *testing.T) {
ctx := logTestContext(t, []string{"test"})
2023-08-06 12:47:47 -06:00
n1_pub, n1_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
2023-08-06 12:47:47 -06:00
n2_pub, n2_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
2023-08-06 12:47:47 -06:00
n1_id := KeyID(n1_pub)
n2_id := KeyID(n2_pub)
ctx.Log.Logf("test", "N1: %s", n1_id)
ctx.Log.Logf("test", "N2: %s", n2_id)
n1_policy := NewPerNodePolicy(map[NodeID]Tree{
n2_id: {
2024-03-03 15:45:45 -07:00
SerializedType(SignalTypeFor[ReadSignal]()): nil,
},
})
2023-08-11 16:00:36 -06:00
n2_listener := NewListenerExt(10)
2024-03-03 15:45:45 -07:00
n2, err := NewNode(ctx, n2_key, "Base", 10, nil, NewGroupExt(nil), n2_listener)
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
2024-03-03 15:45:45 -07:00
n1, err := NewNode(ctx, n1_key, "Base", 10, []Policy{n1_policy}, NewGroupExt(nil))
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
2023-08-01 20:55:15 -06:00
read_sig := NewReadSignal(map[ExtType][]string{
2024-03-03 15:45:45 -07:00
ExtTypeFor[GroupExt](): {"members"},
2023-08-01 20:55:15 -06:00
})
msgs := Messages{}
msgs = msgs.Add(ctx, n1.ID, n2, nil, read_sig)
err = ctx.Send(msgs)
fatalErr(t, err)
res, err := WaitForSignal(n2_listener.Chan, 10*time.Millisecond, func(sig *ReadResultSignal) bool {
return true
})
fatalErr(t, err)
ctx.Log.Logf("test", "READ_RESULT: %+v", res)
}