From 4ce2a642c5e69804e0771177799cbbb64ecc5d06 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 15 Aug 2023 19:28:15 -0600 Subject: [PATCH] Changed TestLink to test add and remove --- lockable.go | 3 +++ lockable_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lockable.go b/lockable.go index 3c6d0b1..c3ff000 100644 --- a/lockable.go +++ b/lockable.go @@ -196,6 +196,9 @@ func (ext *LockableExt) HandleLinkSignal(log Logger, node *Node, source NodeID, if exists == true { msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_requirement"), source) } else { + if ext.Requirements == nil { + ext.Requirements = map[NodeID]ReqState{} + } ext.Requirements[id] = Unlocked msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "req_added"), source) } diff --git a/lockable_test.go b/lockable_test.go index 9076099..e10cb90 100644 --- a/lockable_test.go +++ b/lockable_test.go @@ -18,7 +18,7 @@ func lockableTestContext(t *testing.T, logs []string) *Context { } func TestLink(t *testing.T) { - ctx := lockableTestContext(t, []string{"listener"}) + ctx := lockableTestContext(t, []string{"lockable"}) l1_pub, l1_key, err := ed25519.GenerateKey(rand.Reader) fatalErr(t, err) @@ -39,10 +39,20 @@ func TestLink(t *testing.T) { l1_listener := NewListenerExt(10) l1 := NewNode(ctx, l1_key, TestLockableType, 10, nil, l1_listener, - NewLockableExt([]NodeID{l2.ID}), + NewLockableExt(nil), ) msgs := Messages{} + msgs = msgs.Add(l1.ID, l1.Key, NewLinkSignal("add", l2.ID), l1.ID) + err = ctx.Send(msgs) + fatalErr(t, err) + + _, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, ErrorSignalType, func(sig *ErrorSignal) bool { + return sig.Error == "req_added" + }) + fatalErr(t, err) + + msgs = Messages{} s := NewBaseSignal("TEST", Down) msgs = msgs.Add(l1.ID, l1.Key, &s, l1.ID) err = ctx.Send(msgs) @@ -57,9 +67,36 @@ func TestLink(t *testing.T) { return sig.ID() == s.ID() }) fatalErr(t, err) + + msgs = Messages{} + msgs = msgs.Add(l1.ID, l1.Key, NewLinkSignal("remove", l2.ID), l1.ID) + err = ctx.Send(msgs) + fatalErr(t, err) + + _, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, ErrorSignalType, func(sig *ErrorSignal) bool { + return sig.Error == "req_removed" + }) + fatalErr(t, err) + + msgs = Messages{} + s = NewBaseSignal("TEST", Down) + msgs = msgs.Add(l1.ID, l1.Key, &s, l1.ID) + err = ctx.Send(msgs) + fatalErr(t, err) + + _, err = WaitForSignal(l1_listener.Chan, time.Millisecond*10, "TEST", func(sig *BaseSignal) bool { + return sig.ID() == s.ID() + }) + fatalErr(t, err) + + select { + case <- l2_listener.Chan: + t.Fatal("Recevied message on l2 after removing link") + default: + } } -func TestLink10K(t *testing.T) { +func Test10KLink(t *testing.T) { ctx := lockableTestContext(t, []string{"test"}) l_pub, listener_key, err := ed25519.GenerateKey(rand.Reader)