graphvent/lockable_test.go

149 lines
4.0 KiB
Go

2023-07-27 16:21:27 -06:00
package graphvent
import (
"testing"
"time"
2023-07-27 16:21:27 -06:00
)
func TestLink(t *testing.T) {
ctx := logTestContext(t, []string{"lockable", "listener"})
2023-08-11 16:00:36 -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)
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)
msgs := []Message{{l1.ID, link_signal}}
2024-03-04 17:30:42 -07:00
err = ctx.Send(l1, msgs)
fatalErr(t, err)
_, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, link_signal.ID())
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)
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)
_, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10, unlink_signal.ID())
fatalErr(t, err)
}
2024-03-30 15:42:06 -06:00
func Test10Lock(t *testing.T) {
testLockN(t, 10)
}
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) {
ctx := logTestContext(t, []string{"test"})
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)
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) {
new_lockable := NewLockable()
reqs[i] = new_lockable.ID
}
2024-03-30 15:42:06 -06:00
ctx.Log.Logf("test", "CREATED_%d", n)
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)
ctx.Log.Logf("test", "CREATED_LISTENER")
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)
}
func TestLock(t *testing.T) {
ctx := logTestContext(t, []string{"test", "lockable"})
2023-08-11 16:00:36 -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)
return l, listener
}
l2, _ := NewLockable(nil)
l3, _ := NewLockable(nil)
l4, _ := NewLockable(nil)
l5, _ := NewLockable(nil)
l0, l0_listener := NewLockable([]NodeID{l5.ID})
l1, l1_listener := NewLockable([]NodeID{l2.ID, l3.ID, l4.ID, l5.ID})
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
ctx.Log.Logf("test", "locking l0")
id_1, err := LockLockable(ctx, l0)
fatalErr(t, err)
2024-03-23 02:21:27 -06:00
response, _, err := WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_1)
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
ctx.Log.Logf("test", "locking l1")
id_2, err := LockLockable(ctx, l1)
fatalErr(t, err)
2024-03-28 20:28:07 -06:00
response, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*10000, id_2)
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
ctx.Log.Logf("test", "unlocking l0")
id_3, err := UnlockLockable(ctx, l0)
fatalErr(t, err)
2024-03-23 02:21:27 -06:00
response, _, err = WaitForResponse(l0_listener.Chan, time.Millisecond*10, id_3)
fatalErr(t, err)
2024-03-23 02:21:27 -06:00
ctx.Log.Logf("test", "l0 unlock: %+v", response)
ctx.Log.Logf("test", "locking l1")
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)
fatalErr(t, err)
2024-03-23 02:21:27 -06:00
ctx.Log.Logf("test", "l1 lock: %+v", response)
}