-
Notifications
You must be signed in to change notification settings - Fork 207
/
test_stellar_rpc_healthy.go
67 lines (55 loc) · 1.15 KB
/
test_stellar_rpc_healthy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
const timeout = 6 * time.Minute
type RPCResponse struct {
Result struct {
Status string `json:"status"`
} `json:"result"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}
func main() {
startTime := time.Now()
getHealthRPCRequest := []byte(`{
"jsonrpc": "2.0",
"id": 10235,
"method": "getHealth"
}`)
for {
time.Sleep(5 * time.Second)
logLine("Waiting for Stellar RPC to start")
if time.Since(startTime) > timeout {
logLine("Timeout")
os.Exit(-1)
}
resp, err := http.Post("http://localhost:8000/rpc", "application/json", bytes.NewBuffer(getHealthRPCRequest))
if err != nil {
logLine(err)
continue
}
var rpcResponse RPCResponse
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&rpcResponse)
if err != nil {
logLine(err)
continue
}
logLine(fmt.Sprintf("Stellar RPC health reponse %#v", rpcResponse))
if rpcResponse.Result.Status == "healthy" {
logLine("Stellar RPC is healthy!")
os.Exit(0)
}
}
}
func logLine(text interface{}) {
log.Println("\033[32;1m[test]\033[0m", text)
}