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 lockableTestContext(t *testing.T, logs []string) *Context {
|
|
|
|
ctx := logTestContext(t, logs)
|
2023-07-27 16:48:39 -06:00
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
err := RegisterNodeType(ctx, "Lockable", []ExtType{ExtTypeFor[LockableExt]()}, map[string]FieldIndex{})
|
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
|
|
|
|
2023-07-27 16:48:39 -06:00
|
|
|
|
|
|
|
l2_listener := NewListenerExt(10)
|
2024-03-04 17:30:42 -07:00
|
|
|
l2, err := NewNode(ctx, nil, "Lockable", 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-04 17:30:42 -07:00
|
|
|
l1, err := NewNode(ctx, nil, "Lockable", 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-04 17:30:42 -07:00
|
|
|
msgs := []SendMsg{{l1.ID, link_signal}}
|
|
|
|
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-04 17:30:42 -07:00
|
|
|
msgs = []SendMsg{{l1.ID, unlink_signal}}
|
|
|
|
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-03 16:38:40 -07:00
|
|
|
func Test1000Lock(t *testing.T) {
|
2023-11-03 22:51:54 -06:00
|
|
|
ctx := lockableTestContext(t, []string{"test", "lockable"})
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-07-28 13:12:17 -06:00
|
|
|
NewLockable := func()(*Node) {
|
2024-03-04 17:30:42 -07:00
|
|
|
l, err := NewNode(ctx, nil, "Lockable", 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-03 16:38:40 -07: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
|
|
|
}
|
2024-03-03 16:38:40 -07:00
|
|
|
ctx.Log.Logf("test", "CREATED_1000")
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-10-30 01:42:36 -06:00
|
|
|
listener := NewListenerExt(5000)
|
2024-03-04 17:30:42 -07:00
|
|
|
node, err := NewNode(ctx, nil, "Lockable", 5000, 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-03 16:38:40 -07:00
|
|
|
ctx.Log.Logf("test", "LOCKED_1000")
|
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
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
NewLockable := func(reqs []NodeID)(*Node, *ListenerExt) {
|
2024-03-03 16:38:40 -07:00
|
|
|
listener := NewListenerExt(1000)
|
2024-03-04 17:30:42 -07:00
|
|
|
l, err := NewNode(ctx, nil, "Lockable", 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
|
|
|
|
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-30 19:40:30 -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-11-03 22:51:54 -06:00
|
|
|
_, _, err = WaitForResponse(l1_listener.Chan, time.Millisecond*100, 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-30 19:40:30 -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)
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
_, _, 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
|
|
|
}
|