offset_request_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package sarama
  2. import "testing"
  3. var (
  4. offsetRequestNoBlocks = []byte{
  5. 0xFF, 0xFF, 0xFF, 0xFF,
  6. 0x00, 0x00, 0x00, 0x00}
  7. offsetRequestOneBlock = []byte{
  8. 0xFF, 0xFF, 0xFF, 0xFF,
  9. 0x00, 0x00, 0x00, 0x01,
  10. 0x00, 0x03, 'f', 'o', 'o',
  11. 0x00, 0x00, 0x00, 0x01,
  12. 0x00, 0x00, 0x00, 0x04,
  13. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  14. 0x00, 0x00, 0x00, 0x02}
  15. offsetRequestOneBlockV1 = []byte{
  16. 0xFF, 0xFF, 0xFF, 0xFF,
  17. 0x00, 0x00, 0x00, 0x01,
  18. 0x00, 0x03, 'b', 'a', 'r',
  19. 0x00, 0x00, 0x00, 0x01,
  20. 0x00, 0x00, 0x00, 0x04,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
  22. offsetRequestReplicaID = []byte{
  23. 0x00, 0x00, 0x00, 0x2a,
  24. 0x00, 0x00, 0x00, 0x00}
  25. )
  26. func TestOffsetRequest(t *testing.T) {
  27. request := new(OffsetRequest)
  28. testRequest(t, "no blocks", request, offsetRequestNoBlocks)
  29. request.AddBlock("foo", 4, 1, 2)
  30. testRequest(t, "one block", request, offsetRequestOneBlock)
  31. }
  32. func TestOffsetRequestV1(t *testing.T) {
  33. request := new(OffsetRequest)
  34. request.Version = 1
  35. testRequest(t, "no blocks", request, offsetRequestNoBlocks)
  36. request.AddBlock("bar", 4, 1, 2) // Last argument is ignored for V1
  37. testRequest(t, "one block", request, offsetRequestOneBlockV1)
  38. }
  39. func TestOffsetRequestReplicaID(t *testing.T) {
  40. request := new(OffsetRequest)
  41. replicaID := int32(42)
  42. request.SetReplicaID(replicaID)
  43. if found := request.ReplicaID(); found != replicaID {
  44. t.Errorf("replicaID: expected %v, found %v", replicaID, found)
  45. }
  46. testRequest(t, "with replica ID", request, offsetRequestReplicaID)
  47. }