2023-06-25 20:20:59 -06:00
|
|
|
package graphvent
|
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
import (
|
2024-03-17 14:25:34 -06:00
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"golang.org/x/net/websocket"
|
2023-06-25 20:20:59 -06:00
|
|
|
)
|
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
|
2024-03-17 14:25:34 -06:00
|
|
|
func TestGQLSubscribe(t *testing.T) {
|
2024-03-23 03:23:00 -06:00
|
|
|
ctx := logTestContext(t, []string{"test"})
|
2024-03-17 14:25:34 -06:00
|
|
|
|
|
|
|
n1, err := NewNode(ctx, nil, "Lockable", 10, NewLockableExt(nil))
|
2023-07-29 00:40:18 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-10 23:43:10 -06:00
|
|
|
|
2023-07-29 00:40:18 -06:00
|
|
|
listener_ext := NewListenerExt(10)
|
2024-03-17 14:25:34 -06:00
|
|
|
|
|
|
|
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-08 14:00:17 -06:00
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
gql, err := NewNode(ctx, nil, "Lockable", 10, NewLockableExt([]NodeID{n1.ID}), gql_ext, listener_ext)
|
2023-08-31 22:31:29 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-10 23:43:10 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "GQL: %s", gql.ID)
|
|
|
|
ctx.Log.Logf("test", "NODE: %s", n1.ID)
|
|
|
|
|
2024-03-17 14:25:34 -06:00
|
|
|
sub_1 := GQLPayload{
|
2024-03-21 14:13:54 -06:00
|
|
|
Query: "subscription { Self { ID, Type ... on Lockable { lockable_state } } }",
|
2023-07-29 11:03:41 -06:00
|
|
|
}
|
2024-03-17 14:25:34 -06:00
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
|
2023-10-10 11:23:44 -06:00
|
|
|
url := fmt.Sprintf("http://localhost:%d/gql", port)
|
|
|
|
ws_url := fmt.Sprintf("ws://127.0.0.1:%d/gqlws", port)
|
2023-07-29 11:03:41 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
SubGQL := func(payload GQLPayload) {
|
|
|
|
config, err := websocket.NewConfig(ws_url, url)
|
|
|
|
fatalErr(t, err)
|
|
|
|
config.Protocol = append(config.Protocol, "graphql-ws")
|
|
|
|
config.TlsConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
|
|
|
|
ws, err := websocket.DialConfig(config)
|
|
|
|
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-11 19:04:43 -06:00
|
|
|
type payload_struct struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
init := struct{
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{
|
|
|
|
uuid.New(),
|
|
|
|
"connection_init",
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ser, err := json.Marshal(&init)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = ws.Write(ser)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
resp := make([]byte, 1024)
|
|
|
|
n, err := ws.Read(resp)
|
|
|
|
|
|
|
|
var init_resp GQLWSMsg
|
|
|
|
err = json.Unmarshal(resp[:n], &init_resp)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
if init_resp.Type != "connection_ack" {
|
|
|
|
t.Fatal("Didn't receive connection_ack")
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := GQLWSMsg{
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
Type: "subscribe",
|
|
|
|
Payload: sub_1,
|
|
|
|
}
|
|
|
|
|
|
|
|
ser, err = json.Marshal(&sub)
|
|
|
|
fatalErr(t, err)
|
|
|
|
_, err = ws.Write(ser)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-09-14 15:50:08 -06:00
|
|
|
n, err = ws.Read(resp)
|
|
|
|
fatalErr(t, err)
|
2024-03-23 03:23:00 -06:00
|
|
|
ctx.Log.Logf("test", "SUB1: %s", resp[:n])
|
2023-09-18 19:55:55 -06:00
|
|
|
|
2024-03-21 14:13:54 -06:00
|
|
|
lock_id, err := LockLockable(ctx, gql)
|
2023-09-18 19:55:55 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-03-21 14:13:54 -06:00
|
|
|
response, _, err := WaitForResponse(listener_ext.Chan, 100*time.Millisecond, lock_id)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
switch response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
ctx.Log.Logf("test", "Locked %s", gql.ID)
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected lock response: %s", response)
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:55:55 -06:00
|
|
|
n, err = ws.Read(resp)
|
|
|
|
fatalErr(t, err)
|
2024-03-23 03:23:00 -06:00
|
|
|
ctx.Log.Logf("test", "SUB2: %s", resp[:n])
|
|
|
|
|
|
|
|
n, err = ws.Read(resp)
|
|
|
|
fatalErr(t, err)
|
|
|
|
ctx.Log.Logf("test", "SUB3: %s", resp[:n])
|
|
|
|
|
|
|
|
n, err = ws.Read(resp)
|
|
|
|
fatalErr(t, err)
|
|
|
|
ctx.Log.Logf("test", "SUB4: %s", resp[:n])
|
2023-10-11 19:04:43 -06:00
|
|
|
|
|
|
|
// TODO: check that there are no more messages sent to ws within a timeout
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
SubGQL(sub_1)
|
2023-07-29 00:40:18 -06:00
|
|
|
}
|
|
|
|
|
2024-03-17 14:25:34 -06:00
|
|
|
func TestGQLQuery(t *testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{"test", "lockable"})
|
|
|
|
|
|
|
|
n1_listener := NewListenerExt(10)
|
|
|
|
n1, err := NewNode(ctx, nil, "Lockable", 10, NewLockableExt(nil), n1_listener)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
gql_listener := NewListenerExt(10)
|
|
|
|
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
gql, err := NewNode(ctx, nil, "Lockable", 10, NewLockableExt([]NodeID{n1.ID}), gql_ext, gql_listener)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "GQL: %s", gql.ID)
|
|
|
|
ctx.Log.Logf("test", "NODE: %s", n1.ID)
|
|
|
|
|
|
|
|
skipVerifyTransport := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: skipVerifyTransport}
|
|
|
|
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
|
|
|
|
url := fmt.Sprintf("http://localhost:%d/gql", port)
|
|
|
|
|
|
|
|
req_1 := GQLPayload{
|
|
|
|
Query: "query Node($id:graphvent_NodeID) { Node(id:$id) { ID, Type, ... on Lockable { lockable_state } } }",
|
|
|
|
Variables: map[string]interface{}{
|
|
|
|
"id": n1.ID.String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req_2 := GQLPayload{
|
|
|
|
Query: "query Self { Self { ID, Type, ... on Lockable { lockable_state, requirements { Key { ID ... on Lockable { lockable_state } } } } } }",
|
|
|
|
}
|
|
|
|
|
|
|
|
SendGQL := func(payload GQLPayload) []byte {
|
|
|
|
ser, err := json.MarshalIndent(&payload, "", " ")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
req_data := bytes.NewBuffer(ser)
|
|
|
|
req, err := http.NewRequest("GET", url, req_data)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
|
|
|
resp_1 := SendGQL(req_1)
|
|
|
|
ctx.Log.Logf("test", "RESP_1: %s", resp_1)
|
|
|
|
resp_2 := SendGQL(req_2)
|
|
|
|
ctx.Log.Logf("test", "RESP_2: %s", resp_2)
|
|
|
|
|
|
|
|
lock_id, err := LockLockable(ctx, n1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
response, _, err := WaitForResponse(n1_listener.Chan, 100*time.Millisecond, lock_id)
|
|
|
|
fatalErr(t, err)
|
|
|
|
switch response := response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Wrong response: %s", reflect.TypeOf(response))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp_3 := SendGQL(req_1)
|
|
|
|
ctx.Log.Logf("test", "RESP_3: %s", resp_3)
|
|
|
|
|
|
|
|
resp_4 := SendGQL(req_2)
|
|
|
|
ctx.Log.Logf("test", "RESP_4: %s", resp_4)
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:18 -06:00
|
|
|
func TestGQLDB(t *testing.T) {
|
2023-11-06 00:50:29 -07:00
|
|
|
ctx := logTestContext(t, []string{"test", "db", "node"})
|
2023-07-26 13:28:03 -06:00
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
|
2023-07-29 00:40:18 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 12:03:39 -06:00
|
|
|
listener_ext := NewListenerExt(10)
|
2023-07-27 00:57:18 -06:00
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
gql, err := NewNode(ctx, nil, "Base", 10, gql_ext, listener_ext)
|
2023-08-08 14:00:17 -06:00
|
|
|
fatalErr(t, err)
|
2024-03-10 16:31:14 -06:00
|
|
|
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
|
|
|
|
|
|
|
|
err = ctx.Unload(gql.ID)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2024-03-10 16:31:14 -06:00
|
|
|
gql_loaded, err := ctx.Load(gql.ID)
|
2023-07-01 13:06:39 -06:00
|
|
|
fatalErr(t, err)
|
2023-11-06 00:50:29 -07:00
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
listener_ext, err = GetExt[ListenerExt](gql_loaded)
|
2023-07-27 12:20:49 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-01 13:06:39 -06:00
|
|
|
}
|