-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test validating if we correctly decode Kafka control records. Signed-off-by: Michal Maslanka <michal@vectorized.io>
- Loading branch information
1 parent
f0000d4
commit 942bd4c
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,67 @@ | ||
package sarama | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var ( | ||
abortTxCtrlRecKey = []byte{ | ||
0, 0, // version | ||
0, 0, // TX_ABORT = 0 | ||
} | ||
abortTxCtrlRecValue = []byte{ | ||
0, 0, // version | ||
0, 0, 0, 10, // coordinator epoch | ||
} | ||
commitTxCtrlRecKey = []byte{ | ||
0, 0, // version | ||
0, 1, // TX_COMMIT = 1 | ||
} | ||
commitTxCtrlRecValue = []byte{ | ||
0, 0, // version | ||
0, 0, 0, 15, // coordinator epoch | ||
} | ||
unknownCtrlRecKey = []byte{ | ||
0, 0, // version | ||
0, 128, // UNKNOWN = -1 | ||
} | ||
// empty value for unknown record | ||
unknownCtrlRecValue = []byte{} | ||
) | ||
|
||
func testDecode(t *testing.T, tp string, key []byte, value []byte) ControlRecord { | ||
controlRecord := ControlRecord{} | ||
err := controlRecord.decode(&realDecoder{raw: key}, &realDecoder{raw: value}) | ||
if err != nil { | ||
t.Error("Decoding control record of type " + tp + " failed") | ||
return ControlRecord{} | ||
} | ||
return controlRecord | ||
} | ||
|
||
func assertRecordType(t *testing.T, r *ControlRecord, expected ControlRecordType) { | ||
if r.Type != expected { | ||
t.Errorf("control record type mismatch, expected: %v, have %v", expected, r.Type) | ||
} | ||
} | ||
|
||
func TestDecodingControlRecords(t *testing.T) { | ||
abortTx := testDecode(t, "abort transaction", abortTxCtrlRecKey, abortTxCtrlRecValue) | ||
|
||
assertRecordType(t, &abortTx, ControlRecordAbort) | ||
|
||
if abortTx.CoordinatorEpoch != 10 { | ||
t.Errorf("abort tx control record coordinator epoch mismatch") | ||
} | ||
|
||
commitTx := testDecode(t, "commit transaction", commitTxCtrlRecKey, commitTxCtrlRecValue) | ||
|
||
if commitTx.CoordinatorEpoch != 15 { | ||
t.Errorf("commit tx control record coordinator epoch mismatch") | ||
} | ||
assertRecordType(t, &commitTx, ControlRecordCommit) | ||
|
||
unknown := testDecode(t, "unknown", unknownCtrlRecKey, unknownCtrlRecValue) | ||
|
||
assertRecordType(t, &unknown, ControlRecordUnknown) | ||
} |