Copilot commented on code in PR #1074: URL: https://github.com/apache/incubator-seata-go/pull/1074#discussion_r2986813198
########## pkg/protocol/codec/undolog_delete_req_codec.go: ########## @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package codec + +import ( + "seata.apache.org/seata-go/v2/pkg/protocol/branch" + "seata.apache.org/seata-go/v2/pkg/protocol/message" + "seata.apache.org/seata-go/v2/pkg/util/bytes" + "seata.apache.org/seata-go/v2/pkg/util/log" +) + +type UndoLogDeleteRequestCodec struct{} + +func (u *UndoLogDeleteRequestCodec) Decode(in []byte) interface{} { + data := message.UndoLogDeleteRequest{} + buf := bytes.NewByteBuffer(in) + + data.ResourceId = bytes.ReadString16Length(buf) + saveDays, err := buf.ReadUint16() + if err != nil { + log.Errorf("failed to read SaveDays: %v", err) + return data + } + data.SaveDays = int16(saveDays) + data.BranchType = branch.BranchType(bytes.ReadByte(buf)) + Review Comment: On decode failure (`ReadUint16` error), the codec logs but still returns a non-nil `UndoLogDeleteRequest` with default/partial fields. That can cause downstream processors to run with corrupted data as if decoding succeeded. Consider returning `nil` (so the message is rejected) or otherwise preventing further processing when decoding fails. ########## pkg/remoting/processor/client/rm_delete_undolog_processor.go: ########## @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package client + +import ( + "context" + "fmt" + + "seata.apache.org/seata-go/v2/pkg/protocol/message" + "seata.apache.org/seata-go/v2/pkg/remoting/getty" + "seata.apache.org/seata-go/v2/pkg/util/log" +) + +func initDeleteUndoLog() { + rmDeleteUndoLogProcessor := &rmDeleteUndoLogProcessor{} + getty.GetGettyClientHandlerInstance().RegisterProcessor(message.MessageTypeRmDeleteUndolog, rmDeleteUndoLogProcessor) +} + +type rmDeleteUndoLogProcessor struct{} + +func (r *rmDeleteUndoLogProcessor) Process(ctx context.Context, rpcMessage message.RpcMessage) error { + req, ok := rpcMessage.Body.(message.UndoLogDeleteRequest) + if !ok { + return fmt.Errorf("invalid message body type: %T", rpcMessage.Body) Review Comment: `Process` returns an error when the body type is unexpected, but `gettyClientHandler.OnMessage` ignores the returned error (it doesn't log or propagate it). This means type-mismatch failures will be silent. Please log the error (or handle it in a way that surfaces it) before returning so it isn't lost at runtime. ```suggestion err := fmt.Errorf("invalid message body type: %T, expected message.UndoLogDeleteRequest", rpcMessage.Body) log.Errorf("rmDeleteUndoLogProcessor.Process: %v", err) return err ``` ########## pkg/remoting/processor/client/rm_delete_undolog_processor_test.go: ########## @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package client + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "seata.apache.org/seata-go/v2/pkg/protocol/branch" + "seata.apache.org/seata-go/v2/pkg/protocol/message" + "seata.apache.org/seata-go/v2/pkg/remoting/getty" +) + +func TestRmDeleteUndoLogProcessor_Process(t *testing.T) { + processor := &rmDeleteUndoLogProcessor{} + + tests := []struct { + name string + rpcMessage message.RpcMessage + wantErr bool + }{ + { + name: "process normal undo log delete request", + rpcMessage: message.RpcMessage{ + ID: 1, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "jdbc:mysql://localhost:3306/seata", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process undo log delete request with empty resource id", + rpcMessage: message.RpcMessage{ + ID: 2, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "", + SaveDays: 10, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process undo log delete request with zero save days", + rpcMessage: message.RpcMessage{ + ID: 3, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource", + SaveDays: 0, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process with oneway request type", + rpcMessage: message.RpcMessage{ + ID: 4, + Type: message.GettyRequestTypeRequestOneway, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := processor.Process(context.Background(), tt.rpcMessage) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestRmDeleteUndoLogProcessor_ProcessWithContext(t *testing.T) { + processor := &rmDeleteUndoLogProcessor{} + + t.Run("background context", func(t *testing.T) { + err := processor.Process(context.Background(), message.RpcMessage{ + ID: 1, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource-1", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }) + assert.NoError(t, err) + }) + + t.Run("canceled context should be handled gracefully", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() // cancel immediately + err := processor.Process(ctx, message.RpcMessage{ + ID: 2, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource-2", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }) + assert.NoError(t, err, "current impl does not use ctx; update when deletion logic is added") + }) +} + +func TestInitDeleteUndoLog(t *testing.T) { + getty.GetGettyClientHandlerInstance() + + assert.NotPanics(t, func() { + initDeleteUndoLog() + }, "initDeleteUndoLog should not panic") +} + +func TestRmDeleteUndoLogProcessor_Integration(t *testing.T) { + RegisterProcessor() + + processor := &rmDeleteUndoLogProcessor{} + + err := processor.Process(context.Background(), message.RpcMessage{ + ID: 100, + Type: message.GettyRequestTypeRequestSync, + Codec: 1, + Body: message.UndoLogDeleteRequest{ Review Comment: This test hard-codes `Codec: 1`. Using `byte(codec.CodecTypeSeata)` (or the appropriate constant) would make the intent clearer and avoid coupling the test to the numeric value of the enum. ########## pkg/remoting/processor/client/rm_delete_undolog_processor_test.go: ########## @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package client + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "seata.apache.org/seata-go/v2/pkg/protocol/branch" + "seata.apache.org/seata-go/v2/pkg/protocol/message" + "seata.apache.org/seata-go/v2/pkg/remoting/getty" +) + +func TestRmDeleteUndoLogProcessor_Process(t *testing.T) { + processor := &rmDeleteUndoLogProcessor{} + + tests := []struct { + name string + rpcMessage message.RpcMessage + wantErr bool + }{ + { + name: "process normal undo log delete request", + rpcMessage: message.RpcMessage{ + ID: 1, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "jdbc:mysql://localhost:3306/seata", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process undo log delete request with empty resource id", + rpcMessage: message.RpcMessage{ + ID: 2, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "", + SaveDays: 10, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process undo log delete request with zero save days", + rpcMessage: message.RpcMessage{ + ID: 3, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource", + SaveDays: 0, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + { + name: "process with oneway request type", + rpcMessage: message.RpcMessage{ + ID: 4, + Type: message.GettyRequestTypeRequestOneway, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := processor.Process(context.Background(), tt.rpcMessage) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestRmDeleteUndoLogProcessor_ProcessWithContext(t *testing.T) { + processor := &rmDeleteUndoLogProcessor{} + + t.Run("background context", func(t *testing.T) { + err := processor.Process(context.Background(), message.RpcMessage{ + ID: 1, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource-1", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }) + assert.NoError(t, err) + }) + + t.Run("canceled context should be handled gracefully", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() // cancel immediately + err := processor.Process(ctx, message.RpcMessage{ + ID: 2, + Type: message.GettyRequestTypeRequestSync, + Body: message.UndoLogDeleteRequest{ + ResourceId: "test-resource-2", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }) + assert.NoError(t, err, "current impl does not use ctx; update when deletion logic is added") + }) +} + +func TestInitDeleteUndoLog(t *testing.T) { + getty.GetGettyClientHandlerInstance() + + assert.NotPanics(t, func() { + initDeleteUndoLog() + }, "initDeleteUndoLog should not panic") +} + +func TestRmDeleteUndoLogProcessor_Integration(t *testing.T) { + RegisterProcessor() + + processor := &rmDeleteUndoLogProcessor{} + + err := processor.Process(context.Background(), message.RpcMessage{ + ID: 100, + Type: message.GettyRequestTypeRequestSync, + Codec: 1, + Body: message.UndoLogDeleteRequest{ + ResourceId: "jdbc:mysql://127.0.0.1:3306/seata_test", + SaveDays: 7, + BranchType: branch.BranchTypeAT, + }, + }) + assert.NoError(t, err) +} Review Comment: `TestRmDeleteUndoLogProcessor_Integration` doesn't actually exercise the processor registration/dispatch path (it instantiates the processor directly and calls `Process`). Either rename this to reflect it's a unit test, or extend it to validate that a registered processor is invoked via the getty handler when a `MessageTypeRmDeleteUndolog` message is received. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
