57 строки
1.4 KiB
Go
57 строки
1.4 KiB
Go
package workercluster
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// jsonMarshal marshals v to JSON or fails the test.
|
|
func jsonMarshal(v interface{}) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// bytesBuffer is a minimal in-memory raft.SnapshotSink that captures
|
|
// the bytes written to it. Used by snapshot Persist() in tests.
|
|
type bytesBuffer struct {
|
|
bytes.Buffer
|
|
}
|
|
|
|
func (b *bytesBuffer) Close() error { return nil }
|
|
func (b *bytesBuffer) Cancel() error { return nil }
|
|
func (b *bytesBuffer) ID() string { return "test-snapshot" }
|
|
|
|
// mustEntry encodes an Entry struct with the given field values into
|
|
// JSON. It exists so the test bodies stay readable. All payload fields
|
|
// are []byte; pass nil to leave them unset.
|
|
func mustEntry(
|
|
t *testing.T,
|
|
typ EntryType,
|
|
setJSON, memberJSON, observeJSON, adoptedJSON, configJSON, stateJSON, outboxJSON []byte,
|
|
) []byte {
|
|
t.Helper()
|
|
|
|
entry := Entry{
|
|
Type: typ,
|
|
Set: rawOrEmpty(setJSON),
|
|
Member: rawOrEmpty(memberJSON),
|
|
Observe: rawOrEmpty(observeJSON),
|
|
Adopted: rawOrEmpty(adoptedJSON),
|
|
Report: rawOrEmpty(configJSON),
|
|
State: rawOrEmpty(stateJSON),
|
|
Outbox: rawOrEmpty(outboxJSON),
|
|
}
|
|
raw, err := json.Marshal(entry)
|
|
if err != nil {
|
|
t.Fatalf("encode entry: %v", err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func rawOrEmpty(b []byte) json.RawMessage {
|
|
if len(b) == 0 {
|
|
return nil
|
|
}
|
|
return json.RawMessage(b)
|
|
}
|