mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/simulations: Add a sanity test case for Node.Config UnmarshalJSON
This commit is contained in:
parent
e7fdfbf923
commit
addb5b1e6e
2 changed files with 41 additions and 4 deletions
|
|
@ -653,16 +653,16 @@ func (n *Node) MarshalJSON() ([]byte, error) {
|
||||||
func (n *Node) UnmarshalJSON(raw []byte) error {
|
func (n *Node) UnmarshalJSON(raw []byte) error {
|
||||||
// TODO: How should we turn back NodeInfo into n.Node?
|
// TODO: How should we turn back NodeInfo into n.Node?
|
||||||
// Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
|
// Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
|
||||||
no := struct {
|
node := struct {
|
||||||
Config *adapters.NodeConfig `json:"config,omitempty"`
|
Config *adapters.NodeConfig `json:"config,omitempty"`
|
||||||
Up bool `json:"up"`
|
Up bool `json:"up"`
|
||||||
}{}
|
}{}
|
||||||
if err := json.Unmarshal(raw, &no); err != nil {
|
if err := json.Unmarshal(raw, &node); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.SetUp(no.Up)
|
n.SetUp(node.Up)
|
||||||
n.Config = no.Config
|
n.Config = node.Config
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -494,6 +494,12 @@ func TestNode_UnmarshalJSON(t *testing.T) {
|
||||||
runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField())
|
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) {
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue