From 3e5b52884f815389eb03b6fb7d98aacd8a98af80 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 8 Apr 2023 15:51:42 -0600 Subject: [PATCH] Added update to lock/unlock for resource --- graph.go | 16 ++++++++++++++++ graph_test.go | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/graph.go b/graph.go index 75d9702..c277161 100644 --- a/graph.go +++ b/graph.go @@ -149,6 +149,8 @@ type BaseResource struct { // When the bottom of a tree is reached(no more children) go back up and set the lock state func (resource * BaseResource) Lock() error { var err error = nil + locked := false + resource.state_lock.Lock() if resource.locked == true { err = errors.New("Resource already locked") @@ -163,15 +165,23 @@ func (resource * BaseResource) Lock() error { } if all_children_locked == true { resource.locked = true + locked = true } } resource.state_lock.Unlock() + + if locked == true { + resource.Update() + } + return err } // Recurse through children, unlocking until no more children func (resource * BaseResource) Unlock() error { var err error = nil + unlocked := false + resource.state_lock.Lock() if resource.locked == false { err = errors.New("Resource already unlocked") @@ -186,9 +196,15 @@ func (resource * BaseResource) Unlock() error { } if all_children_unlocked == true{ resource.locked = false + unlocked = true } } resource.state_lock.Unlock() + + if unlocked == true { + resource.Update() + } + return err } diff --git a/graph_test.go b/graph_test.go index 6891059..080e51c 100644 --- a/graph_test.go +++ b/graph_test.go @@ -161,11 +161,14 @@ func TestLockResource(t * testing.T) { t.Fatal("Failed to add initial tiered resources for test") } + r1_l := r1.UpdateChannel() + // Lock r3(so also r1&r2) err := r3.Lock() if err != nil { t.Fatal("Failed to lock r3") } + (*graph_tester)(t).CheckForNil(r1_l) err = r3.Lock() if err == nil { @@ -186,14 +189,17 @@ func TestLockResource(t * testing.T) { if err != nil { t.Fatal("Failed to unlock r3") } + (*graph_tester)(t).CheckForNil(r1_l) err = r4.Lock() if err != nil { t.Fatal("Failed to lock r4 after unlocking r3") } + (*graph_tester)(t).CheckForNil(r1_l) err = r4.Unlock() if err != nil { t.Fatal("Failed to unlock r4") } + (*graph_tester)(t).CheckForNil(r1_l) }