Adds test files for 6 source files that had no corresponding test:
bufpool_test.go (mining + node), metrics_test.go, version_test.go,
supervisor_test.go, mining_profile_test.go. Each follows
TestFilename_Function_{Good,Bad,Ugly} convention.
Also fixes 2 pre-existing compilation errors:
- ratelimiter_test.go: rl -> rateLimiter (leftover from AX rename)
- worker_test.go: worker.node -> worker.nodeManager (field was renamed)
Co-Authored-By: Charon <charon@lethean.io>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package mining
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestBufpool_MarshalJSON_Good(t *testing.T) {
|
|
data := map[string]string{"key": "value"}
|
|
result, err := MarshalJSON(data)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if len(result) == 0 {
|
|
t.Fatal("expected non-empty result")
|
|
}
|
|
}
|
|
|
|
func TestBufpool_MarshalJSON_Bad(t *testing.T) {
|
|
// Channels cannot be marshalled
|
|
result, err := MarshalJSON(make(chan int))
|
|
if err == nil {
|
|
t.Fatal("expected error for non-marshallable type")
|
|
}
|
|
if result != nil {
|
|
t.Fatalf("expected nil result, got %v", result)
|
|
}
|
|
}
|
|
|
|
func TestBufpool_MarshalJSON_Ugly(t *testing.T) {
|
|
// nil value should produce "null"
|
|
result, err := MarshalJSON(nil)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if string(result) != "null" {
|
|
t.Fatalf("expected \"null\", got %q", string(result))
|
|
}
|
|
}
|
|
|
|
func TestBufpool_UnmarshalJSON_Good(t *testing.T) {
|
|
var target map[string]string
|
|
err := UnmarshalJSON([]byte(`{"key":"value"}`), &target)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if target["key"] != "value" {
|
|
t.Fatalf("expected value \"value\", got %q", target["key"])
|
|
}
|
|
}
|
|
|
|
func TestBufpool_UnmarshalJSON_Bad(t *testing.T) {
|
|
var target map[string]string
|
|
err := UnmarshalJSON([]byte(`not valid json`), &target)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid JSON")
|
|
}
|
|
}
|
|
|
|
func TestBufpool_UnmarshalJSON_Ugly(t *testing.T) {
|
|
// Empty JSON object into a map
|
|
var target map[string]string
|
|
err := UnmarshalJSON([]byte(`{}`), &target)
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if len(target) != 0 {
|
|
t.Fatalf("expected empty map, got %d entries", len(target))
|
|
}
|
|
}
|