Skip to content

Commit 98c036b

Browse files
committed
fix(stage_p2): use stage logger for GenerateLogDirs to ensure consistent logging
feat: add testProduce2 to validate produce request error handling fix: simplify deferred client close by removing wrapper function
1 parent a8ef41f commit 98c036b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

internal/stage_p2.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package internal
2+
3+
import (
4+
"github.com/codecrafters-io/kafka-tester/internal/assertions"
5+
"github.com/codecrafters-io/kafka-tester/internal/kafka_executable"
6+
"github.com/codecrafters-io/kafka-tester/protocol/builder"
7+
"github.com/codecrafters-io/kafka-tester/protocol/common"
8+
"github.com/codecrafters-io/kafka-tester/protocol/kafka_client"
9+
"github.com/codecrafters-io/kafka-tester/protocol/serializer"
10+
"github.com/codecrafters-io/tester-utils/test_case_harness"
11+
)
12+
13+
func testProduce2(stageHarness *test_case_harness.TestCaseHarness) error {
14+
b := kafka_executable.NewKafkaExecutable(stageHarness)
15+
stageLogger := stageHarness.Logger
16+
err := serializer.GenerateLogDirs(stageLogger, []string{})
17+
if err != nil {
18+
return err
19+
}
20+
21+
if err := b.Run(); err != nil {
22+
return err
23+
}
24+
25+
correlationId := getRandomCorrelationId()
26+
27+
client := kafka_client.NewClient("localhost:9092")
28+
if err := client.ConnectWithRetries(b, stageLogger); err != nil {
29+
return err
30+
}
31+
defer client.Close()
32+
33+
recordBatch := builder.NewRecordBatchBuilder().
34+
AddStringRecord(common.MESSAGE1).
35+
Build()
36+
37+
request := builder.NewProduceRequestBuilder().
38+
AddRecordBatch(common.TOPIC_UNKOWN_NAME, 0, recordBatch).
39+
WithCorrelationId(correlationId).
40+
Build()
41+
42+
rawResponse, err := client.SendAndReceive(request, stageLogger)
43+
if err != nil {
44+
return err
45+
}
46+
47+
actualResponse := builder.NewEmptyProduceResponse()
48+
if err := actualResponse.Decode(rawResponse.Payload, stageLogger); err != nil {
49+
return err
50+
}
51+
52+
expectedResponse := builder.NewProduceResponseBuilder().
53+
AddErrorPartitionResponse(common.TOPIC_UNKOWN_NAME, 0, 3).
54+
WithCorrelationId(correlationId).
55+
Build()
56+
57+
if err = assertions.NewProduceResponseAssertion(actualResponse, expectedResponse, stageLogger).Run(stageLogger); err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}

0 commit comments

Comments
 (0)