2023-07-27 16:21:27 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-07-27 18:08:43 -06:00
|
|
|
"time"
|
2023-07-27 16:21:27 -06:00
|
|
|
)
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func TestLink(t *testing.T) {
|
2024-03-10 15:41:13 -06:00
|
|
|
ctx := logTestContext(t, []string{"lockable", "listener"})
|
2023-08-11 16:00:36 -06:00
|
|
|
|
2023-07-27 16:48:39 -06:00
|
|
|
|
|
|
|
l2_listener := NewListenerExt(10)
|
2024-03-28 20:28:07 -06:00
|
|
|
l2, err := NewNode(ctx, nil, "LockableNode", 10, 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)
|
2024-03-28 20:28:07 -06:00
|
|
|
l1, err := NewNode(ctx, nil, "LockableNode", 10, l1_listener, l1_lockable)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 18:16:37 -06:00
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
link_signal := NewLinkSignal("add", l2.ID)
|
2024-03-31 00:36:50 -06:00
|
|
|
msgs := []Message{{l1.ID, link_signal}}
|
2024-03-04 17:30:42 -07:00
|
|
|
err = ctx.Send(l1, msgs)
|
2023-08-15 19:28:15 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-30 19:40:30 -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-11-03 22:51:54 -06:00
|
|
|
state, exists := l1_lockable.Requirements[l2.ID]
|
2023-10-07 23:00:07 -06:00
|
|
|
if exists == false {
|
|
|
|
t.Fatal("l2 not in l1 requirements")
|
2023-11-03 22:51:54 -06:00
|
|
|
} else if state != Unlocked {
|
|
|
|
t.Fatalf("l2 in bad requirement state in l1: %+v", state)
|
2023-10-07 23:00:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
unlink_signal := NewLinkSignal("remove", l2.ID)
|
2024-03-31 00:36:50 -06:00
|
|
|
msgs = []Message{{l1.ID, unlink_signal}}
|
2024-03-04 17:30:42 -07:00
|
|
|
err = ctx.Send(l1, msgs)
|
2023-07-27 18:16:37 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-30 19:40:30 -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
|
|
|
}
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
func Test10Lock(t *testing.T) {
|
|
|
|
testLockN(t, 10)
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
func Test100Lock(t *testing.T) {
|
|
|
|
testLockN(t, 100)
|
|
|
|
}
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
func Test1000Lock(t *testing.T) {
|
|
|
|
testLockN(t, 1000)
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
func Test10000Lock(t *testing.T) {
|
2024-03-30 15:42:06 -06:00
|
|
|
testLockN(t, 10000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLockN(t *testing.T, n int) {
|
2024-03-10 15:41:13 -06:00
|
|
|
ctx := logTestContext(t, []string{"test"})
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-07-28 13:12:17 -06:00
|
|
|
NewLockable := func()(*Node) {
|
2024-03-28 20:28:07 -06:00
|
|
|
l, err := NewNode(ctx, nil, "LockableNode", 10, NewLockableExt(nil))
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 13:12:17 -06:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
reqs := make([]NodeID, n)
|
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
|
|
|
}
|
2024-03-30 15:42:06 -06:00
|
|
|
ctx.Log.Logf("test", "CREATED_%d", n)
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2024-03-28 20:28:07 -06:00
|
|
|
listener := NewListenerExt(50000)
|
|
|
|
node, err := NewNode(ctx, nil, "LockableNode", 500000, 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-11-04 23:21:43 -06:00
|
|
|
response, _, err := WaitForResponse(listener.Chan, time.Second*60, lock_id)
|
2023-08-11 16:00:36 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-11-04 23:21:43 -06:00
|
|
|
switch resp := response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unexpected response to lock - %s", resp)
|
|
|
|
}
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ctx.Log.Logf("test", "LOCKED_%d", n)
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func TestLock(t *testing.T) {
|
2024-03-23 02:51:46 -06:00
|
|
|
ctx := logTestContext(t, []string{"test", "lockable"})
|
2023-08-11 16:00:36 -06:00
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockable := func(reqs []NodeID)(*Node, *ListenerExt) {
|
2024-03-28 20:28:07 -06:00
|
|
|
listener := NewListenerExt(10000)
|
|
|
|
l, err := NewNode(ctx, nil, "LockableNode", 10, listener, NewLockableExt(reqs))
|
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
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
ctx.Log.Logf("test", "locking l0")
|
2023-10-01 20:45:44 -06:00
|
|
|
id_1, err := LockLockable(ctx, l0)
|
2023-07-27 18:37:06 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
response, _, err := WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_1)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
ctx.Log.Logf("test", "l0 lock: %+v", response)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
ctx.Log.Logf("test", "locking l1")
|
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)
|
2024-03-28 20:28:07 -06:00
|
|
|
response, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10000, id_2)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
ctx.Log.Logf("test", "l1 lock: %+v", response)
|
2023-08-15 18:23:06 -06:00
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
ctx.Log.Logf("test", "unlocking l0")
|
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)
|
2024-03-23 02:21:27 -06:00
|
|
|
response, _, err = WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_3)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
ctx.Log.Logf("test", "l0 unlock: %+v", response)
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
ctx.Log.Logf("test", "locking l1")
|
2023-10-01 20:45:44 -06:00
|
|
|
id_4, err := LockLockable(ctx, l1)
|
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
response, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, id_4)
|
2023-07-27 22:25:00 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-23 02:21:27 -06:00
|
|
|
ctx.Log.Logf("test", "l1 lock: %+v", response)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|