-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type GetHealthResponse JsonRpcResponse[string] | ||
|
||
// GetHealthResponse returns the current health of the node. A healthy node is one that is within HEALTH_CHECK_SLOT_DISTANCE slots of the latest cluster confirmed slot. | ||
func (c *RpcClient) GetHealth(ctx context.Context) (JsonRpcResponse[string], error) { | ||
return call[JsonRpcResponse[string]](c, ctx, "getHealth") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/blocto/solana-go-sdk/internal/client_test" | ||
) | ||
|
||
func TestGetHealth(t *testing.T) { | ||
client_test.TestAll( | ||
t, | ||
[]client_test.Param{ | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"ok","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetHealth( | ||
context.TODO(), | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: nil, | ||
Result: "ok", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`, | ||
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32005,"message":"Node is behind by 42 slots","data":{"numSlotsBehind":42}},"id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetHealth( | ||
context.TODO(), | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: &JsonRpcError{ | ||
Code: -32005, | ||
Message: `Node is behind by 42 slots`, | ||
Data: map[string]any{ | ||
"numSlotsBehind": float64(42), | ||
}, | ||
}, | ||
Result: "", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`, | ||
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32005,"message":"Node is unhealthy","data":{}},"id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetHealth( | ||
context.TODO(), | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: &JsonRpcError{ | ||
Code: -32005, | ||
Message: `Node is unhealthy`, | ||
Data: map[string]any{}, | ||
}, | ||
Result: "", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
}, | ||
) | ||
} |