2023-06-23 10:10:25 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"fmt"
|
2023-06-24 19:48:59 -06:00
|
|
|
"time"
|
2023-06-30 13:25:35 -06:00
|
|
|
"encoding/json"
|
2023-06-23 10:10:25 -06:00
|
|
|
)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
func TestNewSimpleLockable(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-30 13:25:35 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test lockable 2", []Lockable{l1})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-30 13:25:35 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{l1, l2}, func(states NodeStateMap) error {
|
|
|
|
l1_state := states[l1.ID()].(LockableState)
|
|
|
|
l2_state := states[l2.ID()].(LockableState)
|
|
|
|
|
|
|
|
l1_deps := len(l1_state.Dependencies())
|
|
|
|
if l1_deps != 1 {
|
|
|
|
return fmt.Errorf("l1 has wront amount of dependencies %d/1", l1_deps)
|
|
|
|
}
|
|
|
|
|
|
|
|
l1_dep1 := l1_state.Dependencies()[0]
|
|
|
|
if l1_dep1.ID() != l2.ID() {
|
|
|
|
return fmt.Errorf("Wrong dependency for l1, %s instead of %s", l1_dep1.ID(), l2.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
l2_reqs := len(l2_state.Requirements())
|
|
|
|
if l2_reqs != 1 {
|
|
|
|
return fmt.Errorf("l2 has wrong amount of requirements %d/1", l2_reqs)
|
|
|
|
}
|
|
|
|
|
|
|
|
l2_req1 := l2_state.Requirements()[0]
|
|
|
|
if l2_req1.ID() != l1.ID() {
|
|
|
|
return fmt.Errorf("Wrong requirement for l2, %s instead of %s", l2_req1.ID(), l1.ID())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestRepeatedChildLockable(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
_, err = NewSimpleLockable(ctx, "Test lockable 2", []Lockable{r1, r1})
|
2023-06-23 10:10:25 -06:00
|
|
|
if err == nil {
|
2023-06-28 23:51:44 -06:00
|
|
|
t.Fatal("Added the same lockable as a requirement twice to the same lockable")
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestLockableSelfLock(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error {
|
|
|
|
return LockLockables(ctx, []Lockable{r1}, r1, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner_id := states[r1.ID()].(LockableState).Owner().ID()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_id != r1.ID() {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of self", owner_id)
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error {
|
|
|
|
return UnlockLockables(ctx, []Lockable{r1}, r1, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner := states[r1.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is not unowned after unlock: %s", owner.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestLockableSelfLockTiered(t * testing.T) {
|
2023-06-28 21:49:23 -06:00
|
|
|
ctx := testContext(t)
|
2023-06-23 10:10:25 -06:00
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r2, err := NewSimpleLockable(ctx, "Test lockable 2", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r3, err := NewSimpleLockable(ctx, "Test lockable 3", []Lockable{r1, r2})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error {
|
|
|
|
return LockLockables(ctx, []Lockable{r3}, r3, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1, r2, r3}, func(states NodeStateMap) (error) {
|
|
|
|
owner_1_id := states[r1.ID()].(LockableState).Owner().ID()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_1_id != r3.ID() {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of r3", owner_1_id)
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
owner_2_id := states[r2.ID()].(LockableState).Owner().ID()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_2_id != r3.ID() {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r2 is owned by %s instead of r3", owner_2_id)
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error {
|
|
|
|
return UnlockLockables(ctx, []Lockable{r3}, r3, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1, r2, r3}, func(states NodeStateMap) (error) {
|
|
|
|
owner_1 := states[r1.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_1 != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is not unowned after unlocking: %s", owner_1.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
owner_2 := states[r2.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_2 != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r2 is not unowned after unlocking: %s", owner_2.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
owner_3 := states[r3.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_3 != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r3 is not unowned after unlocking: %s", owner_3.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestLockableLockOther(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r2, err := NewSimpleLockable(ctx, "Test lockable 2", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r1, r2}, func(nodes NodeMap) (error) {
|
|
|
|
node_state := r2.State().(LockableState)
|
|
|
|
err := LockLockables(ctx, []Lockable{r1}, r2, node_state, nodes)
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
2023-06-28 21:49:23 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner_id := states[r1.ID()].(LockableState).Owner().ID()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_id != r2.ID() {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of r2", owner_id)
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) (error) {
|
|
|
|
node_state := r2.State().(LockableState)
|
|
|
|
err := UnlockLockables(ctx, []Lockable{r1}, r2, node_state, nodes)
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
2023-06-28 21:49:23 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner := states[r1.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of r2", owner.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestLockableLockSimpleConflict(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r2, err := NewSimpleLockable(ctx, "Test lockable 2", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error {
|
|
|
|
return LockLockables(ctx, []Lockable{r1}, r1, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) (error) {
|
|
|
|
node_state := r2.State().(LockableState)
|
|
|
|
err := LockLockables(ctx, []Lockable{r1}, r2, node_state, nodes)
|
2023-06-23 10:10:25 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("r2 took r1's lock from itself")
|
|
|
|
}
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner_id := states[r1.ID()].(LockableState).Owner().ID()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner_id != r1.ID() {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of r1", owner_id)
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error {
|
|
|
|
return UnlockLockables(ctx, []Lockable{r1}, r1, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{r1}, func(states NodeStateMap) (error) {
|
|
|
|
owner := states[r1.ID()].(LockableState).Owner()
|
2023-06-23 10:10:25 -06:00
|
|
|
if owner != nil {
|
2023-06-28 00:48:49 -06:00
|
|
|
return fmt.Errorf("r1 is owned by %s instead of r1", owner.ID())
|
2023-06-23 10:10:25 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 10:10:25 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:56:09 -06:00
|
|
|
func TestLockableLockTieredConflict(t * testing.T) {
|
2023-06-23 10:10:25 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r1, err := NewSimpleLockable(ctx, "Test lockable 1", []Lockable{})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r2, err := NewSimpleLockable(ctx, "Test lockable 2", []Lockable{r1})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
r3, err := NewSimpleLockable(ctx, "Test lockable 3", []Lockable{r1})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) error {
|
|
|
|
return LockLockables(ctx, []Lockable{r2}, r2, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error {
|
|
|
|
return LockLockables(ctx, []Lockable{r3}, r3, nil, nodes)
|
|
|
|
})
|
2023-06-23 10:10:25 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Locked r3 which depends on r1 while r2 which depends on r1 is already locked")
|
|
|
|
}
|
|
|
|
}
|
2023-06-24 19:48:59 -06:00
|
|
|
|
|
|
|
func TestLockableSimpleUpdate(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
update_channel := l1.UpdateChannel(0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
SendUpdate(ctx, l1, NewDirectSignal(l1, "test_update"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l1, 100*time.Millisecond, "Didn't receive test_update sent to l1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockableDownUpdate(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{l1})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
_, err = NewSimpleLockable(ctx, "Test Lockable 3", []Lockable{l2})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
update_channel := l1.UpdateChannel(0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
SendUpdate(ctx, l2, NewDownSignal(l2, "test_update"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l2, 100*time.Millisecond, "Didn't receive test_update on l3 sent on l2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockableUpUpdate(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{l1})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l3, err := NewSimpleLockable(ctx, "Test Lockable 3", []Lockable{l2})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
update_channel := l3.UpdateChannel(0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
SendUpdate(ctx, l2, NewSignal(l2, "test_update"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l2, 100*time.Millisecond, "Didn't receive test_update on l3 sent on l2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOwnerNotUpdatedTwice(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{l1})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
update_channel := l2.UpdateChannel(0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
SendUpdate(ctx, l1, NewSignal(l1, "test_update"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
(*GraphTester)(t).WaitForValue(ctx, update_channel, "test_update", l1, 100*time.Millisecond, "Dicn't received test_update on l2 from l1")
|
|
|
|
(*GraphTester)(t).CheckForNone(update_channel, "Second update received on dependency")
|
|
|
|
}
|
2023-06-28 21:49:23 -06:00
|
|
|
|
|
|
|
func TestLockableDependencyOverlap(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{l1})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
_, err = NewSimpleLockable(ctx, "Test Lockable 3", []Lockable{l1, l2})
|
2023-06-28 21:49:23 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should have thrown an error because of dependency overlap")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLockableDBLoad(t * testing.T){
|
2023-07-01 13:03:28 -06:00
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-02 12:14:04 -06:00
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
l3, err := NewSimpleLockable(ctx, "Test Lockable 3", []Lockable{l1, l2})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
l4, err := NewSimpleLockable(ctx, "Test Lockable 4", []Lockable{l3})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
_, err = NewSimpleLockable(ctx, "Test Lockable 5", []Lockable{l4})
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
l6, err := NewSimpleLockable(ctx, "Test Lockable 6", []Lockable{})
|
2023-06-28 23:51:44 -06:00
|
|
|
err = UpdateStates(ctx, []GraphNode{l6, l3}, func(nodes NodeMap) error {
|
|
|
|
l6_state := l6.State().(LockableState)
|
|
|
|
err := LockLockables(ctx, []Lockable{l3}, l6, l6_state, nodes)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-06-30 13:25:35 -06:00
|
|
|
err = UseStates(ctx, []GraphNode{l3}, func(states NodeStateMap) error {
|
|
|
|
ser, err := json.MarshalIndent(states[l3.ID()], "", " ")
|
|
|
|
fmt.Printf("\n%s\n\n", ser)
|
|
|
|
return err
|
|
|
|
})
|
2023-06-28 21:49:23 -06:00
|
|
|
|
2023-07-01 13:03:28 -06:00
|
|
|
l3_loaded, err := LoadNode(ctx, l3.ID())
|
2023-06-28 21:49:23 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-01 13:03:28 -06:00
|
|
|
|
|
|
|
// TODO: add more equivalence checks
|
|
|
|
err = UseStates(ctx, []GraphNode{l3_loaded}, func(states NodeStateMap) error {
|
|
|
|
ser, err := json.MarshalIndent(states[l3_loaded.ID()], "", " ")
|
|
|
|
fmt.Printf("\n%s\n\n", ser)
|
|
|
|
return err
|
|
|
|
})
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
2023-07-03 16:03:56 -06:00
|
|
|
|
|
|
|
func TestLockableUnlink(t * testing.T){
|
|
|
|
ctx := logTestContext(t, []string{"lockable"})
|
|
|
|
l1, err := NewSimpleLockable(ctx, "Test Lockable 1", []Lockable{})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
l2, err := NewSimpleLockable(ctx, "Test Lockable 2", []Lockable{l1})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
err = UnlinkLockables(ctx, l2, l1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|