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"
"encoding/json"
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-05 23:54:11 -06:00
gql_thread , err := NewGQLThread ( ctx , ":0" , [ ] Lockable { } )
2023-06-25 20:20:59 -06:00
fatalErr ( t , err )
2023-07-02 12:14:04 -06:00
test_thread_1 , err := NewSimpleThread ( ctx , "Test thread 1" , [ ] Lockable { } , BaseThreadActions , BaseThreadHandlers )
2023-06-25 20:20:59 -06:00
fatalErr ( t , err )
2023-07-02 12:14:04 -06:00
test_thread_2 , err := NewSimpleThread ( ctx , "Test thread 2" , [ ] Lockable { } , BaseThreadActions , BaseThreadHandlers )
2023-06-26 21:20:04 -06:00
fatalErr ( t , err )
2023-07-02 12:47:45 -06:00
i1 := NewGQLThreadInfo ( true , "start" , "restore" )
2023-06-26 21:20:04 -06:00
err = LinkThreads ( ctx , gql_thread , test_thread_1 , & i1 )
fatalErr ( t , err )
2023-07-02 12:47:45 -06:00
i2 := NewGQLThreadInfo ( false , "start" , "restore" )
2023-06-26 21:20:04 -06:00
err = LinkThreads ( ctx , gql_thread , test_thread_2 , & i2 )
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-05 23:54:11 -06:00
err := UseStates ( ctx , [ ] GraphNode { thread } , func ( states NodeStateMap ) error {
2023-07-04 18:45:23 -06:00
SendUpdate ( ctx , thread , CancelSignal ( nil ) , states )
return nil
} )
2023-07-05 23:54:11 -06:00
fatalErr ( t , err )
2023-06-25 22:19:05 -06:00
} ( gql_thread )
2023-07-02 12:47:45 -06:00
err = RunThread ( ctx , gql_thread , "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-04 18:45:23 -06:00
ctx := logTestContext ( t , [ ] string { "thread" , "update" , "gql" } )
2023-07-02 12:14:04 -06:00
l1 , err := NewSimpleLockable ( ctx , "Test Lockable 1" , [ ] Lockable { } )
2023-07-01 13:06:39 -06:00
fatalErr ( t , err )
2023-07-03 13:14:48 -06:00
t1 , err := NewSimpleThread ( ctx , "Test Thread 1" , [ ] Lockable { } , BaseThreadActions , BaseThreadHandlers )
2023-07-01 13:06:39 -06:00
fatalErr ( t , err )
2023-07-03 13:14:48 -06:00
update_channel := t1 . UpdateChannel ( 10 )
2023-07-01 13:06:39 -06:00
2023-07-05 23:54:11 -06:00
gql , err := NewGQLThread ( ctx , ":0" , [ ] Lockable { l1 } )
2023-07-01 13:06:39 -06:00
fatalErr ( t , err )
2023-07-03 13:14:48 -06:00
info := NewGQLThreadInfo ( true , "start" , "restore" )
2023-07-04 18:45:23 -06:00
err = UpdateStates ( ctx , [ ] GraphNode { gql , t1 } , func ( nodes NodeMap ) error {
return LinkThreads ( ctx , gql , t1 , & info )
} )
2023-07-03 13:14:48 -06:00
fatalErr ( t , err )
2023-07-04 18:45:23 -06:00
err = UseStates ( ctx , [ ] GraphNode { gql } , func ( states NodeStateMap ) error {
SendUpdate ( ctx , gql , NewSignal ( t1 , "child_added" ) , states )
SendUpdate ( ctx , gql , CancelSignal ( nil ) , states )
return nil
} )
2023-07-03 13:14:48 -06:00
err = RunThread ( ctx , gql , "start" )
fatalErr ( t , err )
2023-07-04 18:45:23 -06:00
( * GraphTester ) ( t ) . WaitForValue ( ctx , update_channel , "thread_done" , t1 , 100 * time . Millisecond , "Didn't receive thread_done from t1 on t1" )
2023-07-03 13:14:48 -06:00
err = UseStates ( ctx , [ ] GraphNode { gql , t1 } , func ( states NodeStateMap ) error {
ser1 , err := json . MarshalIndent ( states [ gql . ID ( ) ] , "" , " " )
ser2 , err := json . MarshalIndent ( states [ t1 . ID ( ) ] , "" , " " )
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-03 13:14:48 -06:00
var t1_loaded * BaseThread = nil
2023-07-01 13:06:39 -06:00
2023-07-03 13:14:48 -06:00
err = UseStates ( ctx , [ ] GraphNode { gql_loaded } , func ( states NodeStateMap ) error {
ser , err := json . MarshalIndent ( states [ gql_loaded . ID ( ) ] , "" , " " )
2023-07-01 13:06:39 -06:00
fmt . Printf ( "\n%s\n\n" , ser )
2023-07-03 13:14:48 -06:00
child := states [ gql_loaded . ID ( ) ] . ( ThreadState ) . Children ( ) [ 0 ]
t1_loaded = child . ( * BaseThread )
update_channel = t1_loaded . UpdateChannel ( 10 )
err = UseMoreStates ( ctx , [ ] GraphNode { child } , states , func ( states NodeStateMap ) error {
ser , err := json . MarshalIndent ( states [ child . ID ( ) ] , "" , " " )
fmt . Printf ( "\n%s\n\n" , ser )
return err
} )
2023-07-04 18:45:23 -06:00
SendUpdate ( ctx , gql_loaded , CancelSignal ( nil ) , states )
2023-07-01 13:06:39 -06:00
return err
} )
2023-07-03 13:14:48 -06:00
err = RunThread ( ctx , gql_loaded . ( Thread ) , "restore" )
fatalErr ( t , err )
( * GraphTester ) ( t ) . WaitForValue ( ctx , update_channel , "thread_done" , t1_loaded , 100 * time . Millisecond , "Dicn't received update_done on t1_loaded from t1_loaded" )
2023-07-01 13:06:39 -06:00
}