2023-06-25 20:20:59 -06:00
package graphvent
import (
"testing"
2023-06-25 22:19:05 -06:00
"time"
2023-07-01 13:06:39 -06:00
"fmt"
2023-07-06 16:57:51 -06:00
"errors"
2023-06-25 20:20:59 -06:00
)
func TestGQLThread ( t * testing . T ) {
2023-07-03 13:14:48 -06:00
ctx := logTestContext ( t , [ ] string { } )
2023-07-09 20:30:19 -06:00
gql_t_r := NewGQLThread ( RandID ( ) , "GQL Thread" , "init" , ":0" )
gql_t := & gql_t_r
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
err := UpdateStates ( ctx , [ ] Node { gql_t , t1 , t2 } , func ( nodes NodeMap ) error {
2023-07-11 16:54:09 -06:00
i1 := NewParentThreadInfo ( true , "start" , "restore" )
2023-07-09 20:30:19 -06:00
err := LinkThreads ( ctx , gql_t , t1 , & i1 , nodes )
if err != nil {
return err
}
2023-06-26 21:20:04 -06:00
2023-07-11 16:54:09 -06:00
i2 := NewParentThreadInfo ( false , "start" , "restore" )
2023-07-09 20:30:19 -06:00
return LinkThreads ( ctx , gql_t , t2 , & i2 , nodes )
} )
2023-06-25 20:20:59 -06:00
fatalErr ( t , err )
2023-06-25 22:19:05 -06:00
go func ( thread Thread ) {
time . Sleep ( 10 * time . Millisecond )
2023-07-09 20:30:19 -06:00
err := UseStates ( ctx , [ ] Node { thread } , func ( nodes NodeMap ) error {
return thread . Signal ( ctx , CancelSignal ( nil ) , nodes )
2023-07-04 18:45:23 -06:00
} )
2023-07-05 23:54:11 -06:00
fatalErr ( t , err )
2023-07-09 20:30:19 -06:00
} ( gql_t )
2023-06-25 22:19:05 -06:00
2023-07-09 20:30:19 -06:00
err = ThreadLoop ( ctx , gql_t , "start" )
2023-06-25 20:20:59 -06:00
fatalErr ( t , err )
}
2023-07-01 13:06:39 -06:00
func TestGQLDBLoad ( t * testing . T ) {
2023-07-09 20:30:19 -06:00
ctx := logTestContext ( t , [ ] string { "thread" , "signal" , "gql" , "test" } )
l1_r := NewSimpleLockable ( RandID ( ) , "Test Lockable 1" )
l1 := & l1_r
2023-07-01 13:06:39 -06:00
2023-07-09 20:30:19 -06:00
t1_r := NewSimpleThread ( RandID ( ) , "Test Thread 1" , "init" , nil , BaseThreadActions , BaseThreadHandlers )
t1 := & t1_r
update_channel := UpdateChannel ( t1 , 10 , "test" )
2023-07-01 13:06:39 -06:00
2023-07-09 20:30:19 -06:00
gql_r := NewGQLThread ( RandID ( ) , "GQL Thread" , "init" , ":8080" )
gql := & gql_r
2023-07-01 13:06:39 -06:00
2023-07-11 16:54:09 -06:00
info := NewParentThreadInfo ( true , "start" , "restore" )
2023-07-10 22:31:43 -06:00
err := UpdateStates ( ctx , [ ] Node { gql , t1 , l1 } , func ( nodes NodeMap ) error {
2023-07-09 20:30:19 -06:00
err := LinkLockables ( ctx , gql , [ ] Lockable { l1 } , nodes )
if err != nil {
return err
}
return LinkThreads ( ctx , gql , t1 , & info , nodes )
2023-07-04 18:45:23 -06:00
} )
2023-07-03 13:14:48 -06:00
fatalErr ( t , err )
2023-07-09 20:30:19 -06:00
err = UseStates ( ctx , [ ] Node { gql } , func ( nodes NodeMap ) error {
err := gql . Signal ( ctx , NewSignal ( t1 , "child_added" ) , nodes )
if err != nil {
return nil
}
return gql . Signal ( ctx , AbortSignal ( nil ) , nodes )
2023-07-04 18:45:23 -06:00
} )
2023-07-09 20:30:19 -06:00
fatalErr ( t , err )
2023-07-06 16:57:51 -06:00
err = ThreadLoop ( ctx , gql , "start" )
if errors . Is ( err , NewThreadAbortedError ( "" ) ) {
ctx . Log . Logf ( "test" , "Main thread aborted by signal: %s" , err )
} else {
fatalErr ( t , err )
}
2023-07-03 13:14:48 -06:00
2023-07-06 16:57:51 -06:00
( * GraphTester ) ( t ) . WaitForValue ( ctx , update_channel , "thread_aborted" , t1 , 100 * time . Millisecond , "Didn't receive thread_abort from t1 on t1" )
2023-07-03 13:14:48 -06:00
2023-07-09 20:30:19 -06:00
err = UseStates ( ctx , [ ] Node { gql , t1 } , func ( nodes NodeMap ) error {
ser1 , err := gql . Serialize ( )
ser2 , err := t1 . Serialize ( )
2023-07-03 13:14:48 -06:00
fmt . Printf ( "\n%s\n\n" , ser1 )
fmt . Printf ( "\n%s\n\n" , ser2 )
2023-07-01 13:06:39 -06:00
return err
} )
2023-07-03 13:14:48 -06:00
gql_loaded , err := LoadNode ( ctx , gql . ID ( ) )
2023-07-01 13:06:39 -06:00
fatalErr ( t , err )
2023-07-09 20:30:19 -06:00
var t1_loaded * SimpleThread = nil
2023-07-01 13:06:39 -06:00
2023-07-09 20:30:19 -06:00
var update_channel_2 chan GraphSignal
err = UseStates ( ctx , [ ] Node { gql_loaded } , func ( nodes NodeMap ) error {
ser , err := gql_loaded . Serialize ( )
2023-07-01 13:06:39 -06:00
fmt . Printf ( "\n%s\n\n" , ser )
2023-07-09 20:30:19 -06:00
child := gql_loaded . ( Thread ) . Children ( ) [ 0 ] . ( * SimpleThread )
t1_loaded = child
update_channel_2 = UpdateChannel ( t1_loaded , 10 , "test" )
err = UseMoreStates ( ctx , [ ] Node { child } , nodes , func ( nodes NodeMap ) error {
ser , err := child . Serialize ( )
2023-07-03 13:14:48 -06:00
fmt . Printf ( "\n%s\n\n" , ser )
return err
} )
2023-07-09 20:30:19 -06:00
gql_loaded . Signal ( ctx , AbortSignal ( nil ) , nodes )
2023-07-01 13:06:39 -06:00
return err
} )
2023-07-03 13:14:48 -06:00
2023-07-06 16:57:51 -06:00
err = ThreadLoop ( ctx , gql_loaded . ( Thread ) , "restore" )
if errors . Is ( err , NewThreadAbortedError ( "" ) ) {
ctx . Log . Logf ( "test" , "Main thread aborted by signal: %s" , err )
} else {
fatalErr ( t , err )
}
2023-07-10 01:07:56 -06:00
( * GraphTester ) ( t ) . WaitForValue ( ctx , update_channel_2 , "thread_aborted" , t1_loaded , 100 * time . Millisecond , "Didn't received thread_aborted on t1_loaded from t1_loaded" )
2023-07-03 13:14:48 -06:00
2023-07-01 13:06:39 -06:00
}