首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用事务运行Go测试

使用事务运行Go测试
EN

Stack Overflow用户
提问于 2019-04-09 05:57:05
回答 1查看 411关注 0票数 0

我有使用FoundationDB的Go代码,我想测试它。

我从字面上复制了Go test from Apple's Github。当我在事务中取消对实际运行Set的代码行的注释时,测试挂起,然后超时。(仍然注释掉这一行,它可以按预期工作。)

代码语言:javascript
复制
func TestExampleTransactor(t *testing.T) {
    fdb.MustAPIVersion(400)  // Note: test behaves the same with MustAPIVersion(600)
    db := fdb.MustOpenDefault()

    setOne := func(t fdb.Transactor, key fdb.Key, value []byte) error {
        fmt.Printf("setOne called with:  %T\n", t)
        _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) {
            // We don't actually call tr.Set here to avoid mutating a real database.
            tr.Set(key, value) // **NOTE** this is the line I uncommented
            return nil, nil
        })
        return e
    }

    setMany := func(t fdb.Transactor, value []byte, keys ...fdb.Key) error {
        fmt.Printf("setMany called with: %T\n", t)
        _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) {
            for _, key := range keys {
                setOne(tr, key, value)
            }
            return nil, nil
        })
        return e
    }

    var e error

    fmt.Println("Calling setOne with a database:")
    e = setOne(db, []byte("foo"), []byte("bar"))
    if e != nil {
        fmt.Println(e)
        return
    }
    fmt.Println("\nCalling setMany with a database:")
    e = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3"))
    if e != nil {
        fmt.Println(e)
        return
    }
}

我运行的是MacOS 10.13.6。我已经安装了FoundationDB,并且我能够使用fdbcli连接到它,以及对它进行读写。

以下是foundation进程:

代码语言:javascript
复制
ps aux | grep foundation
root             12942   1.0  0.3  4488980  46608   ??  S    12:35PM   2:00.06 /usr/local/libexec/fdbserver --cluster_file /usr/local/etc/foundationdb/fdb.cluster --datadir /usr/local/foundationdb/data/4689 --listen_address
root             12517   0.3  0.1  4382548  12204   ??  S    12:32PM   0:29.63 /usr/local/foundationdb/backup_agent/backup_agent --cluster_file /usr/local/etc/foundationdb/fdb.cluster
bancron          25399   0.0  0.0  4258468    200 s005  R+    2:55PM   0:00.00 grep foundation
root             12515   0.0  0.0  4297540    584   ??  Ss   12:32PM   0:00.01 /usr/local/libexec/fdbmonitor --conffile /usr/local/etc/foundationdb/foundationdb.conf --lockfile /var/run/FoundationDB.pid

如果是相关的,则这是位于/usr/local/etc/foundationdb/fdb.cluster的默认集群文件的内容

代码语言:javascript
复制
JCKqKWc6:ku0VRske@127.0.0.1:4689

这是/usr/local/etc/foundationdb/foundationdb.conf的内容

代码语言:javascript
复制
[general]
restart_delay = 60
cluster_file = /usr/local/etc/foundationdb/fdb.cluster

## Default parameters for individual fdbserver processes
[fdbserver]
command = /usr/local/libexec/fdbserver
public_address = auto:$ID
listen_address = public
datadir = /usr/local/foundationdb/data/$ID
logdir = /usr/local/foundationdb/logs

## An individual fdbserver process with id 4689
## Parameters set here override defaults from the [fdbserver] section
[fdbserver.4689]

[backup_agent]
command = /usr/local/foundationdb/backup_agent/backup_agent
logdir = /usr/local/foundationdb/logs

[backup_agent.1]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-10 05:35:36

现在,我先运行一些fdbserver和fdbcli命令,这样就可以工作了。

代码语言:javascript
复制
func TestMain(m *testing.M) {
    port := 4692
    testFileContents := []byte(fmt.Sprintf("test:test%d@127.0.0.1:%d", port, port))
    err := ioutil.WriteFile("fdb_test.cluster", testFileContents, 0755)
    db := fdb.MustOpen("fdb_test.cluster", []byte("DB"))
    mErr := m.Run()
    // ... tests run now

    err = fdbServerCmd.Process.Kill()
    if err != nil {
        log.Fatalf("error killing fdbserver process %s", err)
    }
    os.Exit(mErr)
}

func createDB() *exec.Cmd {
    port := 4692
    testFileContents := []byte(fmt.Sprintf("test:test%d@127.0.0.1:%d", port, port))
    err := ioutil.WriteFile("fdb_test.cluster", testFileContents, 0755)
    if err != nil {
        log.Fatalf("unable to write fdb_test.cluster file %s", err)
    }
    fdbServerLoc := "/usr/local/libexec/fdbserver"
    fdbServerCmd := exec.Command(fdbServerLoc,
        "-p", fmt.Sprintf("127.0.0.1:%d", port),
        "--datadir", fmt.Sprintf("/tmp/fdb%d", port),
        "-C", "fdb_test.cluster",
    )
    fdbServerCmd.Start()
    configureLoc := "/usr/local/bin/fdbcli"
    configureCmd := exec.Command(configureLoc,
        "-C", "fdb_test.cluster", "--exec", "configure new single memory")
    // This may error if the DB already exists (from previous test runs), so ignore the error.
    configureCmd.Run()
    return fdbServerCmd
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55582340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档