Updated to make it easier to create modules

graph-rework-2
noah metz 2023-06-26 01:16:44 -06:00
parent 26f57daf23
commit dc7f742f32
5 changed files with 60 additions and 40 deletions

@ -202,22 +202,23 @@ type GraphNode interface {
SignalChannel() chan GraphSignal SignalChannel() chan GraphSignal
} }
// Create a new base node with the given ID // Create a new base node with a new ID
func NewNode(ctx * GraphContext, id NodeID, state NodeState) BaseNode { func NewNode(ctx * GraphContext, state NodeState) (BaseNode, error) {
node := BaseNode{ node := BaseNode{
id: id, id: RandID(),
signal: make(chan GraphSignal, 512), signal: make(chan GraphSignal, 512),
listeners: map[chan GraphSignal]chan GraphSignal{}, listeners: map[chan GraphSignal]chan GraphSignal{},
state: state, state: state,
} }
err := WriteDBState(ctx, id, state) err := WriteDBState(ctx, node.id, state)
if err != nil { if err != nil {
panic(fmt.Sprintf("DB_NEW_WRITE_ERROR: %s", err)) return node, fmt.Errorf("DB_NEW_WRITE_ERROR: %e", err)
} }
ctx.Log.Logf("graph", "NEW_NODE: %s - %+v", id, state) ctx.Log.Logf("graph", "NEW_NODE: %s - %+v", node.id, state)
return node return node, nil
} }
// BaseNode is the minimum set of fields needed to implement a GraphNode, // BaseNode is the minimum set of fields needed to implement a GraphNode,

@ -429,16 +429,30 @@ func (lockable * BaseLockable) Unlock(node GraphNode, state LockableState) error
return nil return nil
} }
func NewBaseLockable(ctx * GraphContext, name string, requirements []Lockable) (* BaseLockable, error) { func NewBaseLockable(ctx * GraphContext, state LockableState) (BaseLockable, error) {
state := NewBaseLockableState(name) base_node, err := NewNode(ctx, state)
lockable := &BaseLockable{ if err != nil {
BaseNode: NewNode(ctx, RandID(), &state), return BaseLockable{}, err
}
lockable := BaseLockable{
BaseNode: base_node,
} }
err := LinkLockables(ctx, lockable, requirements) return lockable, nil
}
func NewSimpleBaseLockable(ctx * GraphContext, name string, requirements []Lockable) (*BaseLockable, error) {
state := NewBaseLockableState(name)
lockable, err := NewBaseLockable(ctx, &state)
if err != nil {
return nil, err
}
lockable_ptr := &lockable
err = LinkLockables(ctx, lockable_ptr, requirements)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return lockable, nil return lockable_ptr, nil
} }

@ -7,23 +7,23 @@ import (
"time" "time"
) )
func TestNewBaseLockable(t * testing.T) { func TestNewSimpleBaseLockable(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
_, err = NewBaseLockable(ctx, "Test lockable 2", []Lockable{r1}) _, err = NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{r1})
fatalErr(t, err) fatalErr(t, err)
} }
func TestRepeatedChildLockable(t * testing.T) { func TestRepeatedChildLockable(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
_, err = NewBaseLockable(ctx, "Test lockable 2", []Lockable{r1, r1}) _, err = NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{r1, r1})
if err == nil { if err == nil {
t.Fatal("Added the same lockable as a child twice to the same lockable") t.Fatal("Added the same lockable as a child twice to the same lockable")
} }
@ -32,7 +32,7 @@ func TestRepeatedChildLockable(t * testing.T) {
func TestLockableSelfLock(t * testing.T) { func TestLockableSelfLock(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
err = LockLockable(ctx, r1, r1, nil) err = LockLockable(ctx, r1, r1, nil)
@ -64,13 +64,13 @@ func TestLockableSelfLock(t * testing.T) {
func TestLockableSelfLockTiered(t * testing.T) { func TestLockableSelfLockTiered(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
r2, err := NewBaseLockable(ctx, "Test lockable 2", []Lockable{}) r2, err := NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
r3, err := NewBaseLockable(ctx, "Test lockable 3", []Lockable{r1, r2}) r3, err := NewSimpleBaseLockable(ctx, "Test lockable 3", []Lockable{r1, r2})
fatalErr(t, err) fatalErr(t, err)
err = LockLockable(ctx, r3, r3, nil) err = LockLockable(ctx, r3, r3, nil)
@ -120,10 +120,10 @@ func TestLockableSelfLockTiered(t * testing.T) {
func TestLockableLockOther(t * testing.T) { func TestLockableLockOther(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
r2, err := NewBaseLockable(ctx, "Test lockable 2", []Lockable{}) r2, err := NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
_, err = UpdateStates(ctx, []GraphNode{r2}, func(states []NodeState) ([]NodeState, interface{}, error) { _, err = UpdateStates(ctx, []GraphNode{r2}, func(states []NodeState) ([]NodeState, interface{}, error) {
@ -167,10 +167,10 @@ func TestLockableLockOther(t * testing.T) {
func TestLockableLockSimpleConflict(t * testing.T) { func TestLockableLockSimpleConflict(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
r2, err := NewBaseLockable(ctx, "Test lockable 2", []Lockable{}) r2, err := NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
err = LockLockable(ctx, r1, r1, nil) err = LockLockable(ctx, r1, r1, nil)
@ -215,13 +215,13 @@ func TestLockableLockSimpleConflict(t * testing.T) {
func TestLockableLockTieredConflict(t * testing.T) { func TestLockableLockTieredConflict(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
r1, err := NewBaseLockable(ctx, "Test lockable 1", []Lockable{}) r1, err := NewSimpleBaseLockable(ctx, "Test lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
r2, err := NewBaseLockable(ctx, "Test lockable 2", []Lockable{r1}) r2, err := NewSimpleBaseLockable(ctx, "Test lockable 2", []Lockable{r1})
fatalErr(t, err) fatalErr(t, err)
r3, err := NewBaseLockable(ctx, "Test lockable 3", []Lockable{r1}) r3, err := NewSimpleBaseLockable(ctx, "Test lockable 3", []Lockable{r1})
fatalErr(t, err) fatalErr(t, err)
err = LockLockable(ctx, r2, r2, nil) err = LockLockable(ctx, r2, r2, nil)
@ -236,7 +236,7 @@ func TestLockableLockTieredConflict(t * testing.T) {
func TestLockableSimpleUpdate(t * testing.T) { func TestLockableSimpleUpdate(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
l1, err := NewBaseLockable(ctx, "Test Lockable 1", []Lockable{}) l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
update_channel := l1.UpdateChannel(0) update_channel := l1.UpdateChannel(0)
@ -251,13 +251,13 @@ func TestLockableSimpleUpdate(t * testing.T) {
func TestLockableDownUpdate(t * testing.T) { func TestLockableDownUpdate(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
l1, err := NewBaseLockable(ctx, "Test Lockable 1", []Lockable{}) l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
l2, err := NewBaseLockable(ctx, "Test Lockable 2", []Lockable{l1}) l2, err := NewSimpleBaseLockable(ctx, "Test Lockable 2", []Lockable{l1})
fatalErr(t, err) fatalErr(t, err)
_, err = NewBaseLockable(ctx, "Test Lockable 3", []Lockable{l2}) _, err = NewSimpleBaseLockable(ctx, "Test Lockable 3", []Lockable{l2})
fatalErr(t, err) fatalErr(t, err)
update_channel := l1.UpdateChannel(0) update_channel := l1.UpdateChannel(0)
@ -272,13 +272,13 @@ func TestLockableDownUpdate(t * testing.T) {
func TestLockableUpUpdate(t * testing.T) { func TestLockableUpUpdate(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
l1, err := NewBaseLockable(ctx, "Test Lockable 1", []Lockable{}) l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
l2, err := NewBaseLockable(ctx, "Test Lockable 2", []Lockable{l1}) l2, err := NewSimpleBaseLockable(ctx, "Test Lockable 2", []Lockable{l1})
fatalErr(t, err) fatalErr(t, err)
l3, err := NewBaseLockable(ctx, "Test Lockable 3", []Lockable{l2}) l3, err := NewSimpleBaseLockable(ctx, "Test Lockable 3", []Lockable{l2})
fatalErr(t, err) fatalErr(t, err)
update_channel := l3.UpdateChannel(0) update_channel := l3.UpdateChannel(0)
@ -293,10 +293,10 @@ func TestLockableUpUpdate(t * testing.T) {
func TestOwnerNotUpdatedTwice(t * testing.T) { func TestOwnerNotUpdatedTwice(t * testing.T) {
ctx := testContext(t) ctx := testContext(t)
l1, err := NewBaseLockable(ctx, "Test Lockable 1", []Lockable{}) l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
l2, err := NewBaseLockable(ctx, "Test Lockable 2", []Lockable{l1}) l2, err := NewSimpleBaseLockable(ctx, "Test Lockable 2", []Lockable{l1})
fatalErr(t, err) fatalErr(t, err)
update_channel := l2.UpdateChannel(0) update_channel := l2.UpdateChannel(0)

@ -373,8 +373,13 @@ func NewBaseThreadState(name string) BaseThreadState {
} }
func NewBaseThread(ctx * GraphContext, actions ThreadActions, handlers ThreadHandlers, state ThreadState) (BaseThread, error) { func NewBaseThread(ctx * GraphContext, actions ThreadActions, handlers ThreadHandlers, state ThreadState) (BaseThread, error) {
lockable, err := NewBaseLockable(ctx, state)
if err != nil {
return BaseThread{}, err
}
thread := BaseThread{ thread := BaseThread{
BaseLockable: BaseLockable{BaseNode: NewNode(ctx, RandID(), state)}, BaseLockable: lockable,
Actions: ThreadActions{ Actions: ThreadActions{
"wait": ThreadWait, "wait": ThreadWait,
"start": ThreadDefaultStart, "start": ThreadDefaultStart,

@ -34,7 +34,7 @@ func TestNewEvent(t * testing.T) {
func TestEventWithRequirement(t * testing.T) { func TestEventWithRequirement(t * testing.T) {
ctx := logTestContext(t, []string{"lockable", "thread"}) ctx := logTestContext(t, []string{"lockable", "thread"})
l1, err := NewBaseLockable(ctx, "Test Lockable 1", []Lockable{}) l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err) fatalErr(t, err)
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{l1}, ThreadActions{}, ThreadHandlers{}) t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{l1}, ThreadActions{}, ThreadHandlers{})