mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/simulations: fix unmarshal of simulations.Node
Making Node.Up field private in 13292ee897
broke TestHTTPNetwork and TestHTTPSnapshot. Because the default
UnmarshalJSON does not handle unexported fields.
Important: The fix is partial and not proper to my taste. But I cut
scope as I think the fix may require a change to the current
serialization format. New ticket:
https://github.com/ethersphere/go-ethereum/issues/1177
This commit is contained in:
parent
13292ee897
commit
e7fdfbf923
2 changed files with 117 additions and 0 deletions
|
|
@ -647,6 +647,25 @@ func (n *Node) MarshalJSON() ([]byte, error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler interface so that we don't lose
|
||||||
|
// Node.up status. IMPORTANT: The implementation is incomplete; we lose
|
||||||
|
// p2p.NodeInfo.
|
||||||
|
func (n *Node) UnmarshalJSON(raw []byte) error {
|
||||||
|
// TODO: How should we turn back NodeInfo into n.Node?
|
||||||
|
// Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
|
||||||
|
no := struct {
|
||||||
|
Config *adapters.NodeConfig `json:"config,omitempty"`
|
||||||
|
Up bool `json:"up"`
|
||||||
|
}{}
|
||||||
|
if err := json.Unmarshal(raw, &no); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
n.SetUp(no.Up)
|
||||||
|
n.Config = no.Config
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Conn represents a connection between two nodes in the network
|
// Conn represents a connection between two nodes in the network
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
// One is the node which initiated the connection
|
// One is the node which initiated the connection
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -485,3 +486,100 @@ func benchmarkMinimalServiceTmp(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNode_UnmarshalJSON(t *testing.T) {
|
||||||
|
t.Run(
|
||||||
|
"test unmarshal of Node up field",
|
||||||
|
func(t *testing.T) {
|
||||||
|
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) {
|
||||||
|
t.Helper()
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var got Node
|
||||||
|
if err := got.UnmarshalJSON([]byte(tt.marshaled)); err != nil {
|
||||||
|
expectErrorMessageToContain(t, err, tt.wantErr)
|
||||||
|
}
|
||||||
|
expectNodeEquality(t, got, tt.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type nodeUnmarshalTestCase struct {
|
||||||
|
name string
|
||||||
|
marshaled string
|
||||||
|
want Node
|
||||||
|
wantErr string
|
||||||
|
}
|
||||||
|
|
||||||
|
func expectErrorMessageToContain(t *testing.T, got error, want string) {
|
||||||
|
t.Helper()
|
||||||
|
if got == nil && want == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if got == nil && want != "" {
|
||||||
|
t.Errorf("error was expected, got: nil, want: %v", want)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(got.Error(), want) {
|
||||||
|
t.Errorf(
|
||||||
|
"unexpected error message, got %v, want: %v",
|
||||||
|
want,
|
||||||
|
got,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func expectNodeEquality(t *testing.T, got Node, want Node) {
|
||||||
|
t.Helper()
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("Node.UnmarshalJSON() = %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func casesNodeUnmarshalJSONUpField() []nodeUnmarshalTestCase {
|
||||||
|
return []nodeUnmarshalTestCase{
|
||||||
|
{
|
||||||
|
name: "empty json",
|
||||||
|
marshaled: "{}",
|
||||||
|
want: Node{
|
||||||
|
up: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "a stopped node",
|
||||||
|
marshaled: "{\"up\": false}",
|
||||||
|
want: Node{
|
||||||
|
up: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "a running node",
|
||||||
|
marshaled: "{\"up\": true}",
|
||||||
|
want: Node{
|
||||||
|
up: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid JSON value on valid key",
|
||||||
|
marshaled: "{\"up\": foo}",
|
||||||
|
wantErr: "invalid character",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid JSON key and value",
|
||||||
|
marshaled: "{foo: bar}",
|
||||||
|
wantErr: "invalid character",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bool value expected but got something else (string)",
|
||||||
|
marshaled: "{\"up\": \"true\"}",
|
||||||
|
wantErr: "cannot unmarshal string into Go struct",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue