-1

I'm running this test against my redigo functions to see if it supports massive concurrent writes, here're the code

import (
    "github.com/gomodule/redigo/redis"
    "log"
    "os"
)

// Redis connection pool
var RedisPool *redis.Pool

func InitPool() {
    RedisPool = &redis.Pool{
        MaxIdle:   80,
        MaxActive: 12000,
        Dial: func() (redis.Conn, error) {
            conn, err := redis.Dial("tcp", "127.0.0.1:6379")
            if err != nil {
                log.Printf("ERROR: fail init redis: %s", err.Error())
                os.Exit(1)
            }
            return conn, err
        },
    }
}

func ClosePool() error {
    return RedisPool.Close()
}

func Set(key string, val string) error {
    // get conn and put back when exit from method
    conn := RedisPool.Get()
    defer conn.Close()

    _, err := conn.Do("SET", key, val)
    if err != nil {
        log.Printf("ERROR: fail set key %s, val %s, error %s", key, val, err.Error())
        return err
    }

    return nil
}

func TestManySets(t *testing.T) {
    InitPool()
    defer ClosePool()

    var wg sync.WaitGroup
    numOfOperations := 1000
    for i := 0; i < numOfOperations; i++ {
        wg.Add(1)
        go func() {
            err := Set("key", strconv.Itoa(i))
            if err != nil {
                t.Errorf("error when setting key value: %s", err)
            }
            wg.Done()
        }()
    }
    wg.Wait()

    result, err := Get("key")
    if err != nil {
        t.Errorf("error when getting key value: %s", err)
    }
    t.Logf("result: %s", result)
}

When running the test with go test -run TestManySets /path/to/package -count 1 -v I'm getting A LOT of EOF error;

and when running the test with -race, i.e. go test -race -run TestManySets /path/to/package -count 1 -v I'm getting a lot of data race detected, anyone could kindly point out to me how I could make things right?

Below is the data race log:

ycx@DESKTOP-NBD349L:/mnt/c/Users/robbi/Projects/GoGameServer$ go test -race -run TestManySets visiontech.com/adapter -count 1 -v
=== RUN   TestManySets
==================
WARNING: DATA RACE
Read at 0x00c00009ab10 by goroutine 7:
  visiontech.com/adapter.TestManySets.func1()
      /home/ycx/Projects/GoGameServer/src/visiontech.com/adapter/redis_test.go:48 +0x3c

Previous write at 0x00c00009ab10 by goroutine 6:
  visiontech.com/adapter.TestManySets()
      /home/ycx/Projects/GoGameServer/src/visiontech.com/adapter/redis_test.go:45 +0x14c
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163

Goroutine 7 (running) created at:
  visiontech.com/adapter.TestManySets()
      /home/ycx/Projects/GoGameServer/src/visiontech.com/adapter/redis_test.go:47 +0x128
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163

Goroutine 6 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:916 +0x65a
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1157 +0xa8
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1155 +0x523
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1072 +0x2eb
  main.main()
      _testmain.go:62 +0x222
==================
--- FAIL: TestManySets (10.08s)
    redis_test.go:62: result: 1000
    testing.go:809: race detected during execution of test
FAIL
FAIL    visiontech.com/adapter  10.098s
ycx@DESKTOP-NBD349L:/mnt/c/Users/robbi/Projects/GoGameServer$

And redis_test.go line 47 is where the Set happens in the code above.

4

1 回答 1

1

比赛是因为你的 for 循环正在更新i,而你的 goroutines 正在同时读取i。解决这个问题的一种方法是传入i你的 goroutine 函数:

for i := 0; i < numOfOperations; i++ {
    wg.Add(1)
    go func(i int) {                             // <----------- CHANGE THIS
        err := Set("key", strconv.Itoa(i))
        if err != nil {
            t.Errorf("error when setting key value: %s", err)
        }
        wg.Done()
    }(i)                                         // <----------- AND THIS
}

这样你就不再有一个闭包了ii你的 goroutine 函数内部是一个独特的值,可以在不受外部干扰的情况下读取(或写入)。

这也解决了竞争检测器找不到的另一个错误:在for循环中,递增的变量被重复使用,这意味着您当前的版本实际上i在许多情况下无意中使用了相同的值,并跳过了其他值。有关更多信息,请参见此处

于 2019-09-23T12:43:27.637 回答