p2p/simulations: Add a sanity test case for Node.Config UnmarshalJSON

This commit is contained in:
Ferenc Szabo 2019-01-31 12:45:04 +01:00
parent e7fdfbf923
commit addb5b1e6e
2 changed files with 41 additions and 4 deletions

View file

@ -653,16 +653,16 @@ func (n *Node) MarshalJSON() ([]byte, error) {
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 {
node := struct {
Config *adapters.NodeConfig `json:"config,omitempty"`
Up bool `json:"up"`
}{}
if err := json.Unmarshal(raw, &no); err != nil {
if err := json.Unmarshal(raw, &node); err != nil {
return err
}
n.SetUp(no.Up)
n.Config = no.Config
n.SetUp(node.Up)
n.Config = node.Config
return nil
}

View file

@ -494,6 +494,12 @@ func TestNode_UnmarshalJSON(t *testing.T) {
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField())
},
)
t.Run(
"test unmarshal of Node Config field",
func(t *testing.T) {
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONConfig())
},
)
}
func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) {
@ -583,3 +589,34 @@ func casesNodeUnmarshalJSONUpField() []nodeUnmarshalTestCase {
},
}
}
func casesNodeUnmarshalJSONConfig() []nodeUnmarshalTestCase {
// Don't do a big fuss around testing, as adapters.NodeConfig should
// handle it's own serialization. Just do a sanity check.
return []nodeUnmarshalTestCase{
{
name: "missing Config field",
marshaled: "{}",
want: Node{
Config: nil,
},
},
{
name: "Config field is nil",
marshaled: "{\"config\": nil}",
want: Node{
Config: nil,
},
},
{
name: "a non default Config field",
marshaled: "{\"config\":{\"name\":\"node_ecdd0\",\"port\":44665}}",
want: Node{
Config: &adapters.NodeConfig{
Name: "node_ecdd0",
Port: 44665,
},
},
},
}
}