graphvent/lockable_test.go

181 lines
5.3 KiB
Go

2023-07-27 16:21:27 -06:00
package graphvent
import (
"testing"
"time"
2023-07-27 16:21:27 -06:00
)
const TestLockableType = NodeType("TEST_LOCKABLE")
func lockableTestContext(t *testing.T, logs []string) *Context {
ctx := logTestContext(t, logs)
err := ctx.RegisterNodeType(TestLockableType, []ExtType{LockableExtType})
2023-07-27 16:21:27 -06:00
fatalErr(t, err)
return ctx
}
//TODO: add new finer grained signals, and probably add wildcards to not have to deal with annoying acl policies
var link_policy = NewAllNodesPolicy(Actions{MakeAction(LinkSignalType, "*"), MakeAction(StatusSignalType, "+")})
var lock_policy = NewAllNodesPolicy(Actions{MakeAction(LockSignalType, "*")})
func TestLink(t *testing.T) {
2023-07-30 10:09:04 -06:00
ctx := lockableTestContext(t, []string{"lockable"})
l1_listener := NewListenerExt(10)
l1 := NewNode(ctx, nil, TestLockableType, 10, nil,
l1_listener,
NewLockableExt(),
)
l2_listener := NewListenerExt(10)
l2 := NewNode(ctx, nil, TestLockableType, 10, nil,
l2_listener,
NewLockableExt(),
)
// Link l2 as a requirement of l1
err := LinkRequirement(ctx, l1.ID, l2.ID)
fatalErr(t, err)
2023-07-27 16:21:27 -06:00
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LinkSignalType, func(sig *StringSignal) bool {
return sig.Str == "dep_done"
})
fatalErr(t, err)
2023-07-27 18:16:37 -06:00
2023-08-01 20:55:15 -06:00
sig1 := NewStatusSignal("TEST", l2.ID)
err = ctx.Send(l2.ID, []Message{{l2.ID, sig1}})
2023-07-27 18:16:37 -06:00
fatalErr(t, err)
2023-08-01 20:55:15 -06:00
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, StatusSignalType, func(sig *IDStringSignal) bool {
2023-07-30 23:42:47 -06:00
return sig.Str == "TEST"
})
fatalErr(t, err)
2023-08-01 20:55:15 -06:00
_, err = WaitForSignal(ctx, l2_listener, time.Millisecond*10, StatusSignalType, func(sig *IDStringSignal) bool {
2023-07-30 23:42:47 -06:00
return sig.Str == "TEST"
})
fatalErr(t, err)
}
func TestLink10K(t *testing.T) {
ctx := lockableTestContext(t, []string{})
NewLockable := func()(*Node) {
l := NewNode(ctx, nil, TestLockableType, 10, nil,
NewLockableExt(),
)
return l
}
NewListener := func()(*Node, *ListenerExt) {
listener := NewListenerExt(100000)
l := NewNode(ctx, nil, TestLockableType, 256, nil,
listener,
NewLockableExt(),
)
return l, listener
}
l0, l0_listener := NewListener()
lockables := make([]*Node, 10)
for i, _ := range(lockables) {
lockables[i] = NewLockable()
LinkRequirement(ctx, l0.ID, lockables[i].ID)
}
ctx.Log.Logf("test", "CREATED_10K")
for range(lockables) {
_, err := WaitForSignal(ctx, l0_listener, time.Millisecond*10, LinkSignalType, func(sig *StringSignal) bool {
return sig.Str == "dep_done"
})
fatalErr(t, err)
}
ctx.Log.Logf("test", "LINKED_10K")
}
func TestLock(t *testing.T) {
ctx := lockableTestContext(t, []string{})
NewLockable := func()(*Node, *ListenerExt) {
listener := NewListenerExt(100)
l := NewNode(ctx, nil, TestLockableType, 10, nil,
listener,
NewLockableExt(),
)
return l, listener
}
l0, l0_listener := NewLockable()
l1, l1_listener := NewLockable()
l2, _ := NewLockable()
l3, _ := NewLockable()
l4, _ := NewLockable()
l5, _ := NewLockable()
var err error
err = LinkRequirement(ctx, l1.ID, l2.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l1.ID, l3.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l1.ID, l4.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l1.ID, l5.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l0.ID, l2.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l0.ID, l3.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l0.ID, l4.ID)
fatalErr(t, err)
err = LinkRequirement(ctx, l0.ID, l5.ID)
fatalErr(t, err)
linked_as_req := func(sig *StringSignal) bool {
return sig.Str == "dep_done"
}
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"
}
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l0_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l0_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l0_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l0_listener, time.Millisecond*10, LinkSignalType, linked_as_req)
fatalErr(t, err)
err = LockLockable(ctx, l1)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LockSignalType, locked)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LockSignalType, locked)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LockSignalType, locked)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LockSignalType, locked)
fatalErr(t, err)
_, err = WaitForSignal(ctx, l1_listener, time.Millisecond*10, LockSignalType, locked)
fatalErr(t, err)
err = UnlockLockable(ctx, l1)
fatalErr(t, err)
}