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-08-31 19:50:32 -06:00
|
|
|
var TestLockableType = NewNodeType("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-10-07 23:00:07 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"lockable", "listener"})
|
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-09-20 19:14:28 -06:00
|
|
|
l2, err := NewNode(ctx, nil, TestLockableType, 10, []Policy{policy},
|
2023-08-11 16:00:36 -06:00
|
|
|
l2_listener,
|
|
|
|
NewLockableExt(nil),
|
|
|
|
)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-11 16:00:36 -06:00
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
l1_lockable := NewLockableExt(nil)
|
2023-08-11 13:01:32 -06:00
|
|
|
l1_listener := NewListenerExt(10)
|
2023-08-31 22:31:29 -06:00
|
|
|
l1, err := NewNode(ctx, l1_key, TestLockableType, 10, nil,
|
2023-08-11 13:01:32 -06:00
|
|
|
l1_listener,
|
2023-10-07 23:00:07 -06:00
|
|
|
l1_lockable,
|
2023-07-27 16:48:39 -06:00
|
|
|
)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 18:16:37 -06:00
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-10-07 23:00:07 -06:00
|
|
|
link_signal := NewLinkSignal("add", l2.ID)
|
2023-10-14 15:05:23 -06:00
|
|
|
msgs = msgs.Add(ctx, l1.ID, l1, nil, link_signal)
|
2023-08-15 19:28:15 -06:00
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
_, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, link_signal.ID())
|
2023-08-15 19:28:15 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
info, exists := l1_lockable.Requirements[l2.ID]
|
|
|
|
if exists == false {
|
|
|
|
t.Fatal("l2 not in l1 requirements")
|
|
|
|
} else if info.State != Unlocked {
|
|
|
|
t.Fatalf("l2 in bad requirement state in l1: %+v", info.State)
|
|
|
|
}
|
|
|
|
|
2023-08-15 19:28:15 -06:00
|
|
|
msgs = Messages{}
|
2023-10-07 23:00:07 -06:00
|
|
|
unlink_signal := NewLinkSignal("remove", l2.ID)
|
2023-10-14 15:05:23 -06:00
|
|
|
msgs = msgs.Add(ctx, l1.ID, l1, nil, unlink_signal)
|
2023-08-11 16:00:36 -06:00
|
|
|
err = ctx.Send(msgs)
|
2023-07-27 18:16:37 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
_, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, unlink_signal.ID())
|
2023-08-15 19:28:15 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-10-30 01:42:36 -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{
|
2023-09-05 00:08:09 -06:00
|
|
|
SerializedType(LockSignalType): nil,
|
2023-08-11 16:00:36 -06:00
|
|
|
},
|
|
|
|
})
|
2023-07-28 13:12:17 -06:00
|
|
|
NewLockable := func()(*Node) {
|
2023-09-20 19:14:28 -06:00
|
|
|
l, err := NewNode(ctx, nil, TestLockableType, 10, []Policy{child_policy},
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockableExt(nil),
|
2023-07-28 13:12:17 -06:00
|
|
|
)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 13:12:17 -06:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
reqs := make([]NodeID, 1000)
|
2023-10-30 01:42:36 -06:00
|
|
|
for i := range(reqs) {
|
2023-08-11 13:01:32 -06:00
|
|
|
new_lockable := NewLockable()
|
|
|
|
reqs[i] = new_lockable.ID
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
2023-10-30 01:42:36 -06:00
|
|
|
ctx.Log.Logf("test", "CREATED_1K")
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
l_policy := NewAllNodesPolicy(Tree{
|
2023-09-05 00:08:09 -06:00
|
|
|
SerializedType(LockSignalType): nil,
|
2023-08-11 16:00:36 -06:00
|
|
|
})
|
2023-10-01 20:45:44 -06:00
|
|
|
|
2023-10-30 01:42:36 -06:00
|
|
|
listener := NewListenerExt(5000)
|
|
|
|
node, err := NewNode(ctx, listener_key, TestLockableType, 5000, []Policy{l_policy},
|
2023-08-11 16:00:36 -06:00
|
|
|
listener,
|
|
|
|
NewLockableExt(reqs),
|
|
|
|
)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-11 13:01:32 -06:00
|
|
|
ctx.Log.Logf("test", "CREATED_LISTENER")
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
lock_id, err := LockLockable(ctx, node)
|
2023-08-11 16:00:36 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-30 01:42:36 -06:00
|
|
|
_, err = WaitForResponse(listener.Chan, time.Second*60, lock_id)
|
2023-08-11 16:00:36 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-30 01:42:36 -06:00
|
|
|
ctx.Log.Logf("test", "LOCKED_1K")
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func TestLock(t *testing.T) {
|
2023-10-01 20:45:44 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"test", "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-09-20 19:14:28 -06:00
|
|
|
l, err := NewNode(ctx, nil, TestLockableType, 10, []Policy{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
|
|
|
)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
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-10-01 20:45:44 -06:00
|
|
|
l0, l0_listener := NewLockable([]NodeID{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-10-01 20:45:44 -06:00
|
|
|
ctx.Log.Logf("test", "l0: %s", l0.ID)
|
|
|
|
ctx.Log.Logf("test", "l1: %s", l1.ID)
|
|
|
|
ctx.Log.Logf("test", "l2: %s", l2.ID)
|
|
|
|
ctx.Log.Logf("test", "l3: %s", l3.ID)
|
|
|
|
ctx.Log.Logf("test", "l4: %s", l4.ID)
|
|
|
|
ctx.Log.Logf("test", "l5: %s", l5.ID)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
id_1, err := LockLockable(ctx, l0)
|
|
|
|
ctx.Log.Logf("test", "ID_1: %s", id_1)
|
2023-07-27 18:37:06 -06:00
|
|
|
fatalErr(t, err)
|
2023-10-01 20:45:44 -06:00
|
|
|
_, err = WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_1)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
id_2, err := LockLockable(ctx, l1)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-10-01 20:45:44 -06:00
|
|
|
_, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, id_2)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
id_3, err := UnlockLockable(ctx, l0)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-10-01 20:45:44 -06:00
|
|
|
_, err = WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_3)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
id_4, err := LockLockable(ctx, l1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, id_4)
|
2023-07-27 22:25:00 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|