2013-06-24 16:52:51 +00:00
|
|
|
package raft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-06-25 20:29:05 +00:00
|
|
|
"reflect"
|
2013-06-24 16:52:51 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Tests
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//--------------------------------------
|
|
|
|
// Encoding
|
|
|
|
//--------------------------------------
|
|
|
|
|
|
|
|
// Ensure that we can encode a log entry to JSON.
|
|
|
|
func TestLogEntryMarshal(t *testing.T) {
|
2013-07-06 04:49:47 +00:00
|
|
|
e := newLogEntry(nil, 1, 2, &joinCommand{Name: "localhost:1000"})
|
2013-06-24 16:52:51 +00:00
|
|
|
if b, err := json.Marshal(e); !(string(b) == `{"command":{"name":"localhost:1000"},"index":1,"name":"test:join","term":2}` && err == nil) {
|
|
|
|
t.Fatalf("Unexpected log entry marshalling: %v (%v)", string(b), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:29:05 +00:00
|
|
|
// Ensure that we can decode a log entry from JSON.
|
|
|
|
func TestLogEntryUnmarshal(t *testing.T) {
|
|
|
|
e := &LogEntry{}
|
|
|
|
b := []byte(`{"command":{"name":"localhost:1000"},"index":1,"name":"test:join","term":2}`)
|
|
|
|
if err := json.Unmarshal(b, e); err != nil {
|
|
|
|
t.Fatalf("Log entry unmarshalling error: %v", err)
|
|
|
|
}
|
|
|
|
if !(e.Index == 1 && e.Term == 2 && reflect.DeepEqual(e.Command, &joinCommand{Name: "localhost:1000"})) {
|
2013-07-06 04:49:47 +00:00
|
|
|
t.Fatalf("Log entry unmarshaled incorrectly: %v | %v", e, newLogEntry(nil, 1, 2, &joinCommand{Name: "localhost:1000"}))
|
2013-06-25 20:29:05 +00:00
|
|
|
}
|
|
|
|
}
|