bindbox-game/internal/api/activity/lottery_app_test.go

72 lines
1.8 KiB
Go
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"sync/atomic"
"testing"
"time"
)
func TestParseSlotFromRemark(t *testing.T) {
r := parseSlotFromRemark("lottery:activity:1|issue:2|count:1|slot:42")
if r != 42 {
t.Fatalf("slot parse failed: %d", r)
}
r2 := parseSlotFromRemark("lottery:activity:1|issue:2|count:1")
if r2 != -1 {
t.Fatalf("expected -1, got %d", r2)
}
}
// TestShouldTriggerInstantDraw 验证即时开奖触发条件
func TestShouldTriggerInstantDraw(t *testing.T) {
testCases := []struct {
name string
orderStatus int32
drawMode string
shouldTrigger bool
}{
{"已支付+即时开奖", 2, "instant", true},
{"已支付+定时开奖", 2, "scheduled", false},
{"未支付+即时开奖", 1, "instant", false},
{"未支付+定时开奖", 1, "scheduled", false},
{"已取消+即时开奖", 3, "instant", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := shouldTriggerInstantDraw(tc.orderStatus, tc.drawMode)
if result != tc.shouldTrigger {
t.Errorf("期望触发=%v实际触发=%v", tc.shouldTrigger, result)
}
})
}
}
// TestInstantDrawTriggerOnce 验证即时开奖只触发一次
// 这个测试模拟 JoinLottery 中的触发逻辑,确保不会重复触发
func TestInstantDrawTriggerOnce(t *testing.T) {
var callCount int32 = 0
// 模拟 ProcessOrderLottery 的调用
processOrderLottery := func() {
atomic.AddInt32(&callCount, 1)
}
// 模拟订单状态
orderStatus := int32(2)
drawMode := "instant"
// 执行触发逻辑(使用辅助函数,避免重复代码)
if shouldTriggerInstantDraw(orderStatus, drawMode) {
go processOrderLottery()
}
// 等待 goroutine 完成
time.Sleep(100 * time.Millisecond)
// 验证只调用一次
if callCount != 1 {
t.Errorf("ProcessOrderLottery 应该只被调用 1 次,实际调用了 %d 次", callCount)
}
}