From 8596e9b4a08b9abcf5b195d1a56a240874453498 Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Sat, 17 Sep 2022 11:26:54 +0800 Subject: [PATCH] random ip test --- ip_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/ip_test.go b/ip_test.go index b99b339..9173767 100644 --- a/ip_test.go +++ b/ip_test.go @@ -5,8 +5,10 @@ import ( "IPRegion/ip2region" "encoding/json" "fmt" + "math/rand" "sync" "testing" + "time" ) func TestIpInfo(tt *testing.T) { @@ -18,6 +20,48 @@ func TestIpInfo(tt *testing.T) { } +func TestRandomIpInfo(tt *testing.T) { + rand.Seed(time.Now().UnixNano()) + var ipInfo ip2region.IpInfo + for { + address := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255)) + done := make(chan struct{}, 1) + go func() { + fmt.Println(address) + ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType) + res, _ := json.Marshal(ipInfo) + fmt.Println(string(res)) + }() + select { + case <-done: + fmt.Println("call successfully!!!") + continue + case <-time.After(time.Duration(100 * time.Millisecond)): + fmt.Println("timeout!!!") + continue + } + } + +} + +func AsyncCall() { + done := make(chan struct{}, 1) + + //go func(ctx context.Context) { + // // 发送HTTP请求 + // done <- struct{}{} + //}(ctx) + + select { + case <-done: + fmt.Println("call successfully!!!") + return + case <-time.After(time.Duration(100 * time.Millisecond)): + fmt.Println("timeout!!!") + return + } +} + // TestIpRangeChannel 内存测试 func TestIpRangeChannel(tt *testing.T) { ch := make(chan struct{}, 600)