test(wecom): align dedupe rotation behavior and add helper tests
Match rotation semantics to prior behavior by fully resetting the dedupe map once the size limit is exceeded, and add focused tests for duplicate detection and boundary rotation behavior.
This commit is contained in:
parent
18d89937ad
commit
db17cdc86d
2 changed files with 44 additions and 2 deletions
|
|
@ -19,9 +19,9 @@ func markMessageProcessed(msgMu *sync.RWMutex, processedMsgs *map[string]bool, m
|
|||
}
|
||||
(*processedMsgs)[msgID] = true
|
||||
|
||||
// Keep the newest message marker when rotating to bound memory growth.
|
||||
// Keep existing behavior: when over limit, reset dedupe map entirely.
|
||||
if len(*processedMsgs) > maxEntries {
|
||||
*processedMsgs = map[string]bool{msgID: true}
|
||||
*processedMsgs = make(map[string]bool)
|
||||
}
|
||||
|
||||
return true
|
||||
|
|
|
|||
42
pkg/channels/wecom_dedupe_test.go
Normal file
42
pkg/channels/wecom_dedupe_test.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package channels
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMarkMessageProcessed_DuplicateDetection(t *testing.T) {
|
||||
var mu sync.RWMutex
|
||||
processed := make(map[string]bool)
|
||||
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-1", 1000); !ok {
|
||||
t.Fatalf("first message should be accepted")
|
||||
}
|
||||
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-1", 1000); ok {
|
||||
t.Fatalf("duplicate message should be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkMessageProcessed_RotationClearsMapAtBoundary(t *testing.T) {
|
||||
var mu sync.RWMutex
|
||||
processed := make(map[string]bool)
|
||||
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-1", 1); !ok {
|
||||
t.Fatalf("first message should be accepted")
|
||||
}
|
||||
if len(processed) != 1 {
|
||||
t.Fatalf("expected map size 1 after first insert, got %d", len(processed))
|
||||
}
|
||||
|
||||
// Inserting second unique message exceeds maxEntries and should reset map.
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-2", 1); !ok {
|
||||
t.Fatalf("second unique message should be accepted")
|
||||
}
|
||||
if len(processed) != 0 {
|
||||
t.Fatalf("expected map to be reset after rotation, got size %d", len(processed))
|
||||
}
|
||||
if processed["msg-2"] {
|
||||
t.Fatalf("expected current message marker to be cleared after rotation")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue