chore: 初始开源发布
iotlcm-upstream-chan:运营商上游通道适配服务(统一 /sim/* 协议)。 - 内联原私有依赖:yunze-public → internal/pub/(10 包 18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-chan - 修复 mingqian 包测试:实现重构后 `adapter.operations` 字段已移除, 测试断言按当前实现语义重写(不改产品行为) - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、 内网域名、真实手机号、注释里的真实 token/transid - 移除内部资产:CI 触发器、生产通道种子 SQL、真实凭据测试资源文档 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model/entity"
|
||||
)
|
||||
|
||||
func TestTibiotRateLimiterSpacesRequestsAndHonorsCancellation(t *testing.T) {
|
||||
limiter := newTibiotRateLimiter(25 * time.Millisecond)
|
||||
if err := limiter.wait(context.Background()); err != nil {
|
||||
t.Fatalf("first wait failed: %v", err)
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
if err := limiter.wait(context.Background()); err != nil {
|
||||
t.Fatalf("second wait failed: %v", err)
|
||||
}
|
||||
if elapsed := time.Since(started); elapsed < 20*time.Millisecond {
|
||||
t.Fatalf("second slot was granted too early: %s", elapsed)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
||||
defer cancel()
|
||||
reservedBeforeCancel := limiter.next
|
||||
if err := limiter.wait(ctx); err == nil {
|
||||
t.Fatal("expected cancellation while waiting for a slot")
|
||||
}
|
||||
if !limiter.next.Equal(reservedBeforeCancel) {
|
||||
t.Fatalf("canceled waiter reserved a slot: before=%s after=%s", reservedBeforeCancel, limiter.next)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTibiotQuerySimDataUsageRetriesOnceAfterUpstreamFailure(t *testing.T) {
|
||||
previousLimiter := tibiotSimDataMarginLimiter
|
||||
tibiotSimDataMarginLimiter = newTibiotRateLimiter(time.Millisecond)
|
||||
defer func() { tibiotSimDataMarginLimiter = previousLimiter }()
|
||||
|
||||
var calls atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode request: %v", err)
|
||||
}
|
||||
if body["flowUnit"] != "MB" {
|
||||
t.Fatalf("flowUnit missing: %#v", body)
|
||||
}
|
||||
if calls.Add(1) == 1 {
|
||||
_, _ = w.Write([]byte(`{"status":10049,"message":"too many requests","data":null}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"status":0,"message":"ok","data":"2MB"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewSimTibiot().NewTemp(context.Background(), &model.SimChannelConfigItemCache{
|
||||
Template: &entity.TSimChannelTemplate{
|
||||
TempConfig: `{"api":{"baseUrl":"` + server.URL + `"},"http":{"timeout":"2s"}}`,
|
||||
},
|
||||
Channel: &entity.TSimChannels{ChanConfig: `{"userName":"tester","password":"secret"}`},
|
||||
}).(*sSimTibiot)
|
||||
items, err := client.QuerySimDataUsage(context.Background(), &model.SimDataUsageInput{
|
||||
SimKeys: []*model.SimKey{{ICCID: "8986000000000000000"}},
|
||||
})
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("unexpected query result: items=%#v err=%v", items, err)
|
||||
}
|
||||
if calls.Load() != 2 {
|
||||
t.Fatalf("upstream calls = %d, want one retry", calls.Load())
|
||||
}
|
||||
if items[0].Code != 0 || items[0].ResData.DataUsage != 2 {
|
||||
t.Fatalf("unexpected successful retry result: %#v", items[0])
|
||||
}
|
||||
|
||||
cachedItems, err := client.QuerySimDataUsage(context.Background(), &model.SimDataUsageInput{
|
||||
SimKeys: []*model.SimKey{{ICCID: "8986000000000000000"}},
|
||||
})
|
||||
if err != nil || len(cachedItems) != 1 {
|
||||
t.Fatalf("unexpected cached query result: items=%#v err=%v", cachedItems, err)
|
||||
}
|
||||
if calls.Load() != 2 {
|
||||
t.Fatalf("cached query called upstream again: calls=%d", calls.Load())
|
||||
}
|
||||
if !cachedItems[0].Cached || cachedItems[0].UpdatedAt == "" {
|
||||
t.Fatalf("cached query metadata missing: %#v", cachedItems[0])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user