2023-06-23 21:21:14 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-06-23 21:57:26 -06:00
|
|
|
"fmt"
|
2023-06-23 21:21:14 -06:00
|
|
|
)
|
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
func TestNewThread(t * testing.T) {
|
2023-06-23 21:21:14 -06:00
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
t1_r := NewSimpleThread(RandID(), "Test thread 1", "init", nil, BaseThreadActions, BaseThreadHandlers)
|
|
|
|
t1 := &t1_r
|
2023-06-23 21:21:14 -06:00
|
|
|
|
|
|
|
go func(thread Thread) {
|
2023-06-23 21:57:26 -06:00
|
|
|
time.Sleep(10*time.Millisecond)
|
2023-07-09 15:59:41 -06:00
|
|
|
UseStates(ctx, []Node{t1}, func(nodes NodeMap) error {
|
|
|
|
return t1.Signal(ctx, CancelSignal(nil), nodes)
|
2023-07-04 18:45:23 -06:00
|
|
|
})
|
2023-06-23 21:21:14 -06:00
|
|
|
}(t1)
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err := ThreadLoop(ctx, t1, "start")
|
2023-06-23 21:21:14 -06:00
|
|
|
fatalErr(t, err)
|
2023-06-23 21:57:26 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = UseStates(ctx, []Node{t1}, func(nodes NodeMap) (error) {
|
|
|
|
owner := t1.owner
|
2023-06-28 21:49:23 -06:00
|
|
|
if owner != nil {
|
|
|
|
return fmt.Errorf("Wrong owner %+v", owner)
|
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 21:57:26 -06:00
|
|
|
})
|
2023-06-23 21:21:14 -06:00
|
|
|
}
|
2023-06-24 19:48:59 -06:00
|
|
|
|
2023-06-28 21:49:23 -06:00
|
|
|
func TestThreadWithRequirement(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
2023-06-24 19:48:59 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
l1_r := NewSimpleLockable(RandID(), "Test Lockable 1")
|
|
|
|
l1 := &l1_r
|
2023-06-24 19:48:59 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
t1_r := NewSimpleThread(RandID(), "Test Thread 1", "init", nil, BaseThreadActions, BaseThreadHandlers)
|
|
|
|
t1 := &t1_r
|
|
|
|
|
|
|
|
err := UpdateStates(ctx, []Node{l1, t1}, func(nodes NodeMap) error {
|
|
|
|
return LinkLockables(ctx, t1, []Lockable{l1}, nodes)
|
|
|
|
})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
go func (thread Thread) {
|
|
|
|
time.Sleep(10*time.Millisecond)
|
2023-07-09 15:59:41 -06:00
|
|
|
UseStates(ctx, []Node{t1}, func(nodes NodeMap) error {
|
|
|
|
return t1.Signal(ctx, CancelSignal(nil), nodes)
|
2023-07-04 18:45:23 -06:00
|
|
|
})
|
2023-06-24 19:48:59 -06:00
|
|
|
}(t1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-06 16:57:51 -06:00
|
|
|
err = ThreadLoop(ctx, t1, "start")
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = UseStates(ctx, []Node{l1}, func(nodes NodeMap) (error) {
|
|
|
|
owner := l1.owner
|
2023-06-28 21:49:23 -06:00
|
|
|
if owner != nil {
|
|
|
|
return fmt.Errorf("Wrong owner %+v", owner)
|
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-24 19:48:59 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-01 13:03:28 -06:00
|
|
|
|
|
|
|
func TestThreadDBLoad(t * testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-09 15:59:41 -06:00
|
|
|
l1_r := NewSimpleLockable(RandID(), "Test Lockable 1")
|
|
|
|
l1 := &l1_r
|
|
|
|
t1_r := NewSimpleThread(RandID(), "Test Thread 1", "init", nil, BaseThreadActions, BaseThreadHandlers)
|
|
|
|
t1 := &t1_r
|
2023-07-01 13:03:28 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err := UpdateStates(ctx, []Node{t1, l1}, func(nodes NodeMap) error {
|
|
|
|
return LinkLockables(ctx, t1, []Lockable{l1}, nodes)
|
|
|
|
})
|
2023-07-01 13:03:28 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = UseStates(ctx, []Node{t1}, func(nodes NodeMap) error {
|
|
|
|
return t1.Signal(ctx, CancelSignal(nil), nodes)
|
2023-07-04 18:45:23 -06:00
|
|
|
})
|
2023-07-09 15:59:41 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-06 16:57:51 -06:00
|
|
|
err = ThreadLoop(ctx, t1, "start")
|
2023-07-01 13:03:28 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = UseStates(ctx, []Node{t1}, func(nodes NodeMap) error {
|
2023-07-09 16:03:42 -06:00
|
|
|
ser, err := t1.Serialize()
|
2023-07-19 14:45:05 -06:00
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser)
|
2023-07-01 13:03:28 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
t1_loaded, err := LoadNode(ctx, t1.ID())
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = UseStates(ctx, []Node{t1_loaded}, func(nodes NodeMap) error {
|
2023-07-09 16:03:42 -06:00
|
|
|
ser, err := t1_loaded.Serialize()
|
2023-07-19 14:45:05 -06:00
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser)
|
2023-07-01 13:03:28 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2023-07-03 16:03:56 -06:00
|
|
|
|
|
|
|
func TestThreadUnlink(t * testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-09 15:59:41 -06:00
|
|
|
t1_r := NewSimpleThread(RandID(), "Test Thread 1", "init", nil, BaseThreadActions, BaseThreadHandlers)
|
|
|
|
t1 := &t1_r
|
|
|
|
t2_r := NewSimpleThread(RandID(), "Test Thread 2", "init", nil, BaseThreadActions, BaseThreadHandlers)
|
|
|
|
t2 := &t2_r
|
2023-07-03 16:03:56 -06:00
|
|
|
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err := UpdateStates(ctx, []Node{t1, t2}, func(nodes NodeMap) error {
|
|
|
|
err := LinkThreads(ctx, t1, t2, nil, nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-03 16:03:56 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
return UnlinkThreads(ctx, t1, t2)
|
|
|
|
})
|
2023-07-03 16:03:56 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
|