diff --git a/test/clash_test.go b/test/clash_test.go index e1c1c79..88e5fa7 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -11,7 +11,6 @@ import ( "net/netip" "os" "path/filepath" - "sync" "testing" "time" @@ -437,28 +436,31 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { pingCh, pongCh, test := newLargeDataPair() writeRandData := func(pc net.PacketConn, addr net.Addr) (map[int][]byte, error) { hashMap := map[int][]byte{} - mux := sync.Mutex{} + bufs := [][]byte{} + for i := 0; i < times; i++ { - go func(idx int) { - buf := make([]byte, chunkSize) - if _, err := rand.Read(buf[1:]); err != nil { - t.Log(err.Error()) - return - } - buf[0] = byte(idx) + idx := i + buf := make([]byte, chunkSize) + rand.Read(buf[1:]) + buf[0] = byte(idx) - hash := md5.Sum(buf) - mux.Lock() - hashMap[idx] = hash[:] - mux.Unlock() - - if _, err := pc.WriteTo(buf, addr); err != nil { - t.Log(err.Error()) - return - } - }(i) + hash := md5.Sum(buf) + hashMap[idx] = hash[:] + bufs = append(bufs, buf) } + go func() { + cursor := 0 + for { + idx := cursor % times + buf := bufs[idx] + if _, err := pc.WriteTo(buf, addr); err != nil { + return + } + cursor++ + } + }() + return hashMap, nil } @@ -467,15 +469,16 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { hashMap := map[int][]byte{} buf := make([]byte, 64*1024) - for i := 0; i < times; i++ { + for len(hashMap) != times { _, rAddr, err = l.ReadFrom(buf) if err != nil { - t.Log(err.Error()) return } - hash := md5.Sum(buf[:chunkSize]) - hashMap[int(buf[0])] = hash[:] + if _, ok := hashMap[int(buf[0])]; !ok { + hash := md5.Sum(buf[:chunkSize]) + hashMap[int(buf[0])] = hash[:] + } } sendHash, err := writeRandData(l, rAddr) @@ -500,15 +503,16 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { hashMap := map[int][]byte{} buf := make([]byte, 64*1024) - for i := 0; i < times; i++ { + for len(hashMap) != times { _, _, err := pc.ReadFrom(buf) if err != nil { - t.Log(err.Error()) return } - hash := md5.Sum(buf[:chunkSize]) - hashMap[int(buf[0])] = hash[:] + if _, ok := hashMap[int(buf[0])]; !ok { + hash := md5.Sum(buf[:chunkSize]) + hashMap[int(buf[0])] = hash[:] + } } pongCh <- hashPair{ diff --git a/test/dns_test.go b/test/dns_test.go index c378762..f822842 100644 --- a/test/dns_test.go +++ b/test/dns_test.go @@ -63,7 +63,7 @@ dns: fake-ip-filter: - .sslip.io nameserver: - - 119.29.29.29 + - https://doh.pub/dns-query ` err := parseAndApply(basic)