Skip to content

Commit

Permalink
Merge pull request #35 from codecrafters-io/fix-response-logging
Browse files Browse the repository at this point in the history
Introduce Response struct to improve broker response handling
  • Loading branch information
ryan-gang authored Oct 28, 2024
2 parents 234e099 + a7a8cbd commit 586d4dd
Show file tree
Hide file tree
Showing 23 changed files with 418 additions and 401 deletions.
4 changes: 2 additions & 2 deletions internal/stage5.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func testAPIVersion(stageHarness *test_case_harness.TestCaseHarness) error {
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response, 3, logger)
responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response.Payload, 3, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagec1.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func testSequentialRequests(stageHarness *test_case_harness.TestCaseHarness) err
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response, 3, logger)
responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response.Payload, 3, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func testConcurrentRequests(stageHarness *test_case_harness.TestCaseHarness) err
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response, 3, logger)
responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response.Payload, 3, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef1.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func testAPIVersionwFetchKey(stageHarness *test_case_harness.TestCaseHarness) er
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response, 3, logger)
responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response.Payload, 3, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func testFetchWithNoTopics(stageHarness *test_case_harness.TestCaseHarness) erro
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response, 16, logger)
responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response.Payload, 16, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef3.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func testFetchWithUnkownTopicID(stageHarness *test_case_harness.TestCaseHarness)
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response, 16, logger)
responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response.Payload, 16, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef4.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func testFetchNoMessages(stageHarness *test_case_harness.TestCaseHarness) error
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response, 16, logger)
responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response.Payload, 16, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef5.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func testFetchWithSingleMessage(stageHarness *test_case_harness.TestCaseHarness)
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response, 16, logger)
responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response.Payload, 16, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagef6.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func testFetchMultipleMessages(stageHarness *test_case_harness.TestCaseHarness)
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"Fetch\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response, 16, logger)
responseHeader, responseBody, err := kafkaapi.DecodeFetchHeaderAndResponse(response.Payload, 16, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagep1.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func testAPIVersionwDescribeTopicPartitions(stageHarness *test_case_harness.Test
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"ApiVersions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response, 3, logger)
responseHeader, responseBody, err := kafkaapi.DecodeApiVersionsHeaderAndResponse(response.Payload, 3, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagep2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func testDTPartitionWithUnknownTopic(stageHarness *test_case_harness.TestCaseHar
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response, logger)
responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response.Payload, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagep3.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func testDTPartitionWithTopicAndSinglePartition(stageHarness *test_case_harness.
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response, logger)
responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response.Payload, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagep4.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func testDTPartitionWithTopicAndMultiplePartitions2(stageHarness *test_case_harn
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response, logger)
responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response.Payload, logger)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/stagep5.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func testDTPartitionWithTopics(stageHarness *test_case_harness.TestCaseHarness)
if err != nil {
return err
}
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response))
logger.Debugf("Hexdump of received \"DescribeTopicPartitions\" response: \n%v\n", GetFormattedHexdump(response.RawBytes))

responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response, logger)
responseHeader, responseBody, err := kafkaapi.DecodeDescribeTopicPartitionsHeaderAndResponse(response.Payload, logger)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/stages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestStages(t *testing.T) {

func normalizeTesterOutput(testerOutput []byte) []byte {
replacements := map[string][]*regexp.Regexp{
"hexdump": {regexp.MustCompile(`[0-9a-fA-F]{4} \| [0-9a-fA-F ]{47} \| .{0,16}`)},
"hexdump": {regexp.MustCompile(`(?m)(^\x1b\[33m\[stage-\d+\] \x1b\[0m\x1b\[36m[0-9a-f]{4} \| ([a-f0-9][a-f0-9] ){1,16} *\| [[:ascii:]]{1,16}\x1b\[0m\n?)+`)},
"session_id": {regexp.MustCompile(`- .session_id \([0-9]{0,16}\)`)},
"leader_id": {regexp.MustCompile(`- .leader_id \([-0-9]{1,}\)`)},
"leader_epoch": {regexp.MustCompile(`- .leader_epoch \([-0-9]{1,}\)`)},
Expand Down
72 changes: 36 additions & 36 deletions internal/test_helpers/fixtures/base/pass
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,40 @@ Debug = true
[stage-5] Hexdump of received "ApiVersions" response: 
[stage-5] Idx | Hex | ASCII
[stage-5] -----+-------------------------------------------------+-----------------
[stage-5] 0000 | 0a 95 c1 bf 00 00 3e 00 00 00 00 00 0b 00 00 01 | ......>.........
[stage-5] 0010 | 00 00 00 11 00 00 02 00 00 00 09 00 00 03 00 00 | ................
[stage-5] 0020 | 00 0c 00 00 08 00 00 00 09 00 00 09 00 00 00 09 | ................
[stage-5] 0030 | 00 00 0a 00 00 00 06 00 00 0b 00 00 00 09 00 00 | ................
[stage-5] 0040 | 0c 00 00 00 04 00 00 0d 00 00 00 05 00 00 0e 00 | ................
[stage-5] 0050 | 00 00 05 00 00 0f 00 00 00 05 00 00 10 00 00 00 | ................
[stage-5] 0060 | 05 00 00 11 00 00 00 01 00 00 12 00 00 00 04 00 | ................
[stage-5] 0070 | 00 13 00 00 00 07 00 00 14 00 00 00 06 00 00 15 | ................
[stage-5] 0080 | 00 00 00 02 00 00 16 00 00 00 05 00 00 17 00 00 | ................
[stage-5] 0090 | 00 04 00 00 18 00 00 00 05 00 00 19 00 00 00 04 | ................
[stage-5] 00a0 | 00 00 1a 00 00 00 04 00 00 1b 00 00 00 01 00 00 | ................
[stage-5] 00b0 | 1c 00 00 00 04 00 00 1d 00 00 00 03 00 00 1e 00 | ................
[stage-5] 00c0 | 00 00 03 00 00 1f 00 00 00 03 00 00 20 00 00 00 | ............ ...
[stage-5] 00d0 | 04 00 00 21 00 00 00 02 00 00 22 00 00 00 02 00 | ...!......".....
[stage-5] 00e0 | 00 23 00 00 00 04 00 00 24 00 00 00 02 00 00 25 | .#......$......%
[stage-5] 00f0 | 00 00 00 03 00 00 26 00 00 00 03 00 00 27 00 00 | ......&......'..
[stage-5] 0100 | 00 02 00 00 28 00 00 00 02 00 00 29 00 00 00 03 | ....(......)....
[stage-5] 0110 | 00 00 2a 00 00 00 02 00 00 2b 00 00 00 02 00 00 | ..*......+......
[stage-5] 0120 | 2c 00 00 00 01 00 00 2d 00 00 00 00 00 00 2e 00 | ,......-........
[stage-5] 0130 | 00 00 00 00 00 2f 00 00 00 00 00 00 30 00 00 00 | ...../......0...
[stage-5] 0140 | 01 00 00 31 00 00 00 01 00 00 32 00 00 00 00 00 | ...1......2.....
[stage-5] 0150 | 00 33 00 00 00 00 00 00 37 00 00 00 02 00 00 39 | .3......7......9
[stage-5] 0160 | 00 00 00 01 00 00 3c 00 00 00 01 00 00 3d 00 00 | ......<......=..
[stage-5] 0170 | 00 00 00 00 40 00 00 00 00 00 00 41 00 00 00 00 | ....@......A....
[stage-5] 0180 | 00 00 42 00 00 00 01 00 00 44 00 00 00 00 00 00 | ..B......D......
[stage-5] 0190 | 45 00 00 00 00 00 00 4a 00 00 00 00 00 00 4b 00 | E......J......K.
[stage-5] 01a0 | 00 00 00 00 00 50 00 00 00 00 00 00 51 00 00 00 | .....P......Q...
[stage-5] 01b0 | 00 00 00 00 00 00 03 00 3d 04 0e 67 72 6f 75 70 | ........=..group
[stage-5] 01c0 | 2e 76 65 72 73 69 6f 6e 00 00 00 01 00 0e 6b 72 | .version......kr
[stage-5] 01d0 | 61 66 74 2e 76 65 72 73 69 6f 6e 00 00 00 01 00 | aft.version.....
[stage-5] 01e0 | 11 6d 65 74 61 64 61 74 61 2e 76 65 72 73 69 6f | .metadata.versio
[stage-5] 01f0 | 6e 00 01 00 16 00 01 08 00 00 00 00 00 00 00 0c | n...............
[stage-5] 0200 | 02 17 02 11 6d 65 74 61 64 61 74 61 2e 76 65 72 | ....metadata.ver
[stage-5] 0210 | 73 69 6f 6e 00 14 00 14 00 | sion.....
[stage-5] 0000 | 00 00 02 19 0a 95 c1 bf 00 00 3e 00 00 00 00 00 | ..........>.....
[stage-5] 0010 | 0b 00 00 01 00 00 00 11 00 00 02 00 00 00 09 00 | ................
[stage-5] 0020 | 00 03 00 00 00 0c 00 00 08 00 00 00 09 00 00 09 | ................
[stage-5] 0030 | 00 00 00 09 00 00 0a 00 00 00 06 00 00 0b 00 00 | ................
[stage-5] 0040 | 00 09 00 00 0c 00 00 00 04 00 00 0d 00 00 00 05 | ................
[stage-5] 0050 | 00 00 0e 00 00 00 05 00 00 0f 00 00 00 05 00 00 | ................
[stage-5] 0060 | 10 00 00 00 05 00 00 11 00 00 00 01 00 00 12 00 | ................
[stage-5] 0070 | 00 00 04 00 00 13 00 00 00 07 00 00 14 00 00 00 | ................
[stage-5] 0080 | 06 00 00 15 00 00 00 02 00 00 16 00 00 00 05 00 | ................
[stage-5] 0090 | 00 17 00 00 00 04 00 00 18 00 00 00 05 00 00 19 | ................
[stage-5] 00a0 | 00 00 00 04 00 00 1a 00 00 00 04 00 00 1b 00 00 | ................
[stage-5] 00b0 | 00 01 00 00 1c 00 00 00 04 00 00 1d 00 00 00 03 | ................
[stage-5] 00c0 | 00 00 1e 00 00 00 03 00 00 1f 00 00 00 03 00 00 | ................
[stage-5] 00d0 | 20 00 00 00 04 00 00 21 00 00 00 02 00 00 22 00 | ......!......".
[stage-5] 00e0 | 00 00 02 00 00 23 00 00 00 04 00 00 24 00 00 00 | .....#......$...
[stage-5] 00f0 | 02 00 00 25 00 00 00 03 00 00 26 00 00 00 03 00 | ...%......&.....
[stage-5] 0100 | 00 27 00 00 00 02 00 00 28 00 00 00 02 00 00 29 | .'......(......)
[stage-5] 0110 | 00 00 00 03 00 00 2a 00 00 00 02 00 00 2b 00 00 | ......*......+..
[stage-5] 0120 | 00 02 00 00 2c 00 00 00 01 00 00 2d 00 00 00 00 | ....,......-....
[stage-5] 0130 | 00 00 2e 00 00 00 00 00 00 2f 00 00 00 00 00 00 | ........./......
[stage-5] 0140 | 30 00 00 00 01 00 00 31 00 00 00 01 00 00 32 00 | 0......1......2.
[stage-5] 0150 | 00 00 00 00 00 33 00 00 00 00 00 00 37 00 00 00 | .....3......7...
[stage-5] 0160 | 02 00 00 39 00 00 00 01 00 00 3c 00 00 00 01 00 | ...9......<.....
[stage-5] 0170 | 00 3d 00 00 00 00 00 00 40 00 00 00 00 00 00 41 | .=......@......A
[stage-5] 0180 | 00 00 00 00 00 00 42 00 00 00 01 00 00 44 00 00 | ......B......D..
[stage-5] 0190 | 00 00 00 00 45 00 00 00 00 00 00 4a 00 00 00 00 | ....E......J....
[stage-5] 01a0 | 00 00 4b 00 00 00 00 00 00 50 00 00 00 00 00 00 | ..K......P......
[stage-5] 01b0 | 51 00 00 00 00 00 00 00 00 00 03 00 3d 04 0e 67 | Q...........=..g
[stage-5] 01c0 | 72 6f 75 70 2e 76 65 72 73 69 6f 6e 00 00 00 01 | roup.version....
[stage-5] 01d0 | 00 0e 6b 72 61 66 74 2e 76 65 72 73 69 6f 6e 00 | ..kraft.version.
[stage-5] 01e0 | 00 00 01 00 11 6d 65 74 61 64 61 74 61 2e 76 65 | .....metadata.ve
[stage-5] 01f0 | 72 73 69 6f 6e 00 01 00 16 00 01 08 00 00 00 00 | rsion...........
[stage-5] 0200 | 00 00 00 0d 02 17 02 11 6d 65 74 61 64 61 74 61 | ........metadata
[stage-5] 0210 | 2e 76 65 72 73 69 6f 6e 00 14 00 14 00 | .version.....
[stage-5] 
[stage-5] [Decoder] - .ResponseHeader
[stage-5] [Decoder]  - .correlation_id (177586623)
Expand Down Expand Up @@ -446,7 +446,7 @@ Debug = true
[stage-3] 01d0 | 00 0e 6b 72 61 66 74 2e 76 65 72 73 69 6f 6e 00 | ..kraft.version.
[stage-3] 01e0 | 00 00 01 00 11 6d 65 74 61 64 61 74 61 2e 76 65 | .....metadata.ve
[stage-3] 01f0 | 72 73 69 6f 6e 00 01 00 16 00 01 08 00 00 00 00 | rsion...........
[stage-3] 0200 | 00 00 00 0c 02 17 02 11 6d 65 74 61 64 61 74 61 | ........metadata
[stage-3] 0200 | 00 00 00 0d 02 17 02 11 6d 65 74 61 64 61 74 61 | ........metadata
[stage-3] 0210 | 2e 76 65 72 73 69 6f 6e 00 14 00 14 00 | .version.....
[stage-3] 
[stage-3] [Decoder] - .Response
Expand Down Expand Up @@ -505,7 +505,7 @@ Debug = true
[stage-2] 01d0 | 00 0e 6b 72 61 66 74 2e 76 65 72 73 69 6f 6e 00 | ..kraft.version.
[stage-2] 01e0 | 00 00 01 00 11 6d 65 74 61 64 61 74 61 2e 76 65 | .....metadata.ve
[stage-2] 01f0 | 72 73 69 6f 6e 00 01 00 16 00 01 08 00 00 00 00 | rsion...........
[stage-2] 0200 | 00 00 00 0c 02 17 02 11 6d 65 74 61 64 61 74 61 | ........metadata
[stage-2] 0200 | 00 00 00 0d 02 17 02 11 6d 65 74 61 64 61 74 61 | ........metadata
[stage-2] 0210 | 2e 76 65 72 73 69 6f 6e 00 14 00 14 00 | .version.....
[stage-2] 
[stage-2] [Decoder] - .Response
Expand Down
Loading

0 comments on commit 586d4dd

Please sign in to comment.