2023-07-27 16:21:27 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-07-27 18:08:43 -06:00
|
|
|
"time"
|
2023-08-11 16:00:36 -06:00
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
2023-07-27 16:21:27 -06:00
|
|
|
)
|
|
|
|
|
2023-07-27 16:48:39 -06:00
|
|
|
const TestLockableType = NodeType("TEST_LOCKABLE")
|
2023-07-27 18:37:06 -06:00
|
|
|
func lockableTestContext(t *testing.T, logs []string) *Context {
|
|
|
|
ctx := logTestContext(t, logs)
|
2023-07-27 16:48:39 -06:00
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
err := ctx.RegisterNodeType(TestLockableType, []ExtType{LockableExtType})
|
2023-07-27 16:21:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 16:48:39 -06:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func TestLink(t *testing.T) {
|
2023-08-15 19:28:15 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"lockable"})
|
2023-08-11 16:00:36 -06:00
|
|
|
|
|
|
|
l1_pub, l1_key, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
fatalErr(t, err)
|
|
|
|
l1_id := KeyID(l1_pub)
|
|
|
|
policy := NewPerNodePolicy(map[NodeID]Tree{
|
|
|
|
l1_id: nil,
|
|
|
|
})
|
2023-07-27 16:48:39 -06:00
|
|
|
|
|
|
|
l2_listener := NewListenerExt(10)
|
2023-08-11 16:00:36 -06:00
|
|
|
l2 := NewNode(ctx, nil, TestLockableType, 10,
|
|
|
|
map[PolicyType]Policy{
|
|
|
|
PerNodePolicyType: &policy,
|
|
|
|
},
|
|
|
|
l2_listener,
|
|
|
|
NewLockableExt(nil),
|
|
|
|
)
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
l1_listener := NewListenerExt(10)
|
2023-08-11 16:00:36 -06:00
|
|
|
l1 := NewNode(ctx, l1_key, TestLockableType, 10, nil,
|
2023-08-11 13:01:32 -06:00
|
|
|
l1_listener,
|
2023-08-15 19:28:15 -06:00
|
|
|
NewLockableExt(nil),
|
2023-07-27 16:48:39 -06:00
|
|
|
)
|
2023-07-27 18:16:37 -06:00
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-08-15 19:28:15 -06:00
|
|
|
msgs = msgs.Add(l1.ID, l1.Key, NewLinkSignal("add", l2.ID), l1.ID)
|
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, ErrorSignalType, func(sig *ErrorSignal) bool {
|
|
|
|
return sig.Error == "req_added"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
msgs = Messages{}
|
2023-08-11 16:00:36 -06:00
|
|
|
s := NewBaseSignal("TEST", Down)
|
|
|
|
msgs = msgs.Add(l1.ID, l1.Key, &s, l1.ID)
|
|
|
|
err = ctx.Send(msgs)
|
2023-07-27 18:16:37 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-12 11:26:19 -06:00
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, "TEST", func(sig *BaseSignal) bool {
|
2023-08-11 16:00:36 -06:00
|
|
|
return sig.ID() == s.ID()
|
2023-07-28 19:32:27 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-12 11:26:19 -06:00
|
|
|
_, err = WaitForSignal(l2_listener.Chan, time.Millisecond*10, "TEST", func(sig *BaseSignal) bool {
|
2023-08-11 16:00:36 -06:00
|
|
|
return sig.ID() == s.ID()
|
2023-07-28 19:32:27 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-08-15 19:28:15 -06:00
|
|
|
|
|
|
|
msgs = Messages{}
|
|
|
|
msgs = msgs.Add(l1.ID, l1.Key, NewLinkSignal("remove", l2.ID), l1.ID)
|
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, ErrorSignalType, func(sig *ErrorSignal) bool {
|
|
|
|
return sig.Error == "req_removed"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
msgs = Messages{}
|
|
|
|
s = NewBaseSignal("TEST", Down)
|
|
|
|
msgs = msgs.Add(l1.ID, l1.Key, &s, l1.ID)
|
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, "TEST", func(sig *BaseSignal) bool {
|
|
|
|
return sig.ID() == s.ID()
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <- l2_listener.Chan:
|
|
|
|
t.Fatal("Recevied message on l2 after removing link")
|
|
|
|
default:
|
|
|
|
}
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-08-15 19:28:15 -06:00
|
|
|
func Test10KLink(t *testing.T) {
|
2023-08-11 16:00:36 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"test"})
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
l_pub, listener_key, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
fatalErr(t, err)
|
|
|
|
listener_id := KeyID(l_pub)
|
|
|
|
child_policy := NewPerNodePolicy(map[NodeID]Tree{
|
|
|
|
listener_id: Tree{
|
|
|
|
LockSignalType.String(): nil,
|
|
|
|
},
|
|
|
|
})
|
2023-07-28 13:12:17 -06:00
|
|
|
NewLockable := func()(*Node) {
|
2023-08-11 16:00:36 -06:00
|
|
|
l := NewNode(ctx, nil, TestLockableType, 10,
|
|
|
|
map[PolicyType]Policy{
|
|
|
|
PerNodePolicyType: &child_policy,
|
|
|
|
},
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockableExt(nil),
|
2023-07-28 13:12:17 -06:00
|
|
|
)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
reqs := make([]NodeID, 1000)
|
2023-08-11 13:01:32 -06:00
|
|
|
for i, _ := range(reqs) {
|
|
|
|
new_lockable := NewLockable()
|
|
|
|
reqs[i] = new_lockable.ID
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
2023-07-28 13:12:17 -06:00
|
|
|
ctx.Log.Logf("test", "CREATED_10K")
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
l_policy := NewAllNodesPolicy(Tree{
|
|
|
|
LockSignalType.String(): nil,
|
|
|
|
})
|
|
|
|
listener := NewListenerExt(100000)
|
|
|
|
node := NewNode(ctx, listener_key, TestLockableType, 10000,
|
|
|
|
map[PolicyType]Policy{
|
|
|
|
AllNodesPolicyType: &l_policy,
|
|
|
|
},
|
|
|
|
listener,
|
|
|
|
NewLockableExt(reqs),
|
|
|
|
)
|
2023-08-11 13:01:32 -06:00
|
|
|
ctx.Log.Logf("test", "CREATED_LISTENER")
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
_, err = LockLockable(ctx, node, node.ID)
|
2023-08-11 16:00:36 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-12 11:26:19 -06:00
|
|
|
_, err = WaitForSignal(listener.Chan, time.Millisecond*1000, LockSignalType, func(sig *StringSignal) bool {
|
2023-08-11 16:00:36 -06:00
|
|
|
return sig.Str == "locked"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
for _, _ = range(reqs) {
|
2023-08-12 11:26:19 -06:00
|
|
|
_, err := WaitForSignal(listener.Chan, time.Millisecond*100, LockSignalType, func(sig *StringSignal) bool {
|
2023-08-11 16:00:36 -06:00
|
|
|
return sig.Str == "locked"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("test", "LOCKED_10K")
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func TestLock(t *testing.T) {
|
2023-08-15 18:23:06 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"lockable"})
|
2023-08-11 16:00:36 -06:00
|
|
|
|
|
|
|
policy := NewAllNodesPolicy(nil)
|
2023-07-27 19:53:43 -06:00
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockable := func(reqs []NodeID)(*Node, *ListenerExt) {
|
2023-07-28 12:46:06 -06:00
|
|
|
listener := NewListenerExt(100)
|
2023-08-11 16:00:36 -06:00
|
|
|
l := NewNode(ctx, nil, TestLockableType, 10,
|
|
|
|
map[PolicyType]Policy{
|
|
|
|
AllNodesPolicyType: &policy,
|
|
|
|
},
|
2023-07-27 19:53:43 -06:00
|
|
|
listener,
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockableExt(reqs),
|
2023-07-27 19:53:43 -06:00
|
|
|
)
|
|
|
|
return l, listener
|
|
|
|
}
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
l2, _ := NewLockable(nil)
|
|
|
|
l3, _ := NewLockable(nil)
|
|
|
|
l4, _ := NewLockable(nil)
|
|
|
|
l5, _ := NewLockable(nil)
|
2023-08-15 18:23:06 -06:00
|
|
|
l0, l0_listener := NewLockable([]NodeID{l2.ID, l3.ID, l4.ID, l5.ID})
|
2023-08-11 13:01:32 -06:00
|
|
|
l1, l1_listener := NewLockable([]NodeID{l2.ID, l3.ID, l4.ID, l5.ID})
|
2023-07-28 19:32:27 -06:00
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
locked := func(sig *StringSignal) bool {
|
2023-07-30 23:42:47 -06:00
|
|
|
return sig.Str == "locked"
|
2023-07-28 19:32:27 -06:00
|
|
|
}
|
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
unlocked := func(sig *StringSignal) bool {
|
|
|
|
return sig.Str == "unlocked"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := LockLockable(ctx, l0, l5.ID)
|
2023-07-27 18:37:06 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
_, err = WaitForSignal(l0_listener.Chan, time.Millisecond*10, LockSignalType, locked)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
|
|
|
id, err := LockLockable(ctx, l1, l1.ID)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, ErrorSignalType, func(sig *ErrorSignal) bool {
|
|
|
|
return sig.Error == "not_unlocked" && sig.ReqID() == id
|
|
|
|
})
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
|
|
|
_, err = UnlockLockable(ctx, l0, l5.ID)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
_, err = WaitForSignal(l0_listener.Chan, time.Millisecond*10, LockSignalType, unlocked)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
_, err = LockLockable(ctx, l1, l1.ID)
|
2023-07-27 22:25:00 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
_, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, LockSignalType, func(sig *StringSignal) bool {
|
|
|
|
return sig.Str == "locked"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|