コード例 #1
0
ファイル: mysqlkv_test.go プロジェクト: kristofer/camlistore
// TestMySQLKV tests against a real MySQL instance, using a Docker container.
func TestMySQLKV(t *testing.T) {
	dbname := "camlitest_" + osutil.Username()
	containerID, ip := dockertest.SetupMySQLContainer(t, dbname)
	defer containerID.KillRemove(t)

	kv, err := sorted.NewKeyValue(jsonconfig.Obj{
		"type":     "mysql",
		"host":     ip + ":3306",
		"database": dbname,
		"user":     dockertest.MySQLUsername,
		"password": dockertest.MySQLPassword,
	})
	if err != nil {
		t.Fatalf("mysql.NewKeyValue = %v", err)
	}
	kvtest.TestSorted(t, kv)
}
コード例 #2
0
ファイル: mysql_test.go プロジェクト: camlistore/camlistore
func newMySQLSorted(t *testing.T) (kv sorted.KeyValue, clean func()) {
	dbname := "camlitest_" + osutil.Username()
	containerID, ip := dockertest.SetupMySQLContainer(t, dbname)

	kv, err := sorted.NewKeyValue(jsonconfig.Obj{
		"type":     "mysql",
		"host":     ip + ":3306",
		"database": dbname,
		"user":     dockertest.MySQLUsername,
		"password": dockertest.MySQLPassword,
	})
	if err != nil {
		containerID.KillRemove(t)
		t.Fatal(err)
	}
	return kv, func() {
		kv.Close()
		containerID.KillRemove(t)
	}
}
コード例 #3
0
// TestMySQLKV tests against a real MySQL instance, using a Docker container.
func TestMySQLKV(t *testing.T) {
	dbname := "camlitest_" + osutil.Username()
	containerID, ip := dockertest.SetupMySQLContainer(t, dbname)
	defer containerID.KillRemove(t)

	// TODO(mpl): add test for serverVersion once we host the docker image ourselves
	// (and hence have the control over the version).

	kv, err := sorted.NewKeyValue(jsonconfig.Obj{
		"type":     "mysql",
		"host":     ip + ":3306",
		"database": dbname,
		"user":     dockertest.MySQLUsername,
		"password": dockertest.MySQLPassword,
	})
	if err != nil {
		t.Fatalf("mysql.NewKeyValue = %v", err)
	}
	kvtest.TestSorted(t, kv)
}
コード例 #4
0
func TestRollback(t *testing.T) {
	dbname := "camlitest_" + osutil.Username()
	containerID, ip := dockertest.SetupMySQLContainer(t, dbname)
	defer containerID.KillRemove(t)

	kv, err := sorted.NewKeyValue(jsonconfig.Obj{
		"type":     "mysql",
		"host":     ip + ":3306",
		"database": dbname,
		"user":     dockertest.MySQLUsername,
		"password": dockertest.MySQLPassword,
	})
	if err != nil {
		t.Fatalf("mysql.NewKeyValue = %v", err)
	}

	tooLargeAKey := make([]byte, sorted.MaxKeySize+10)
	for i := range tooLargeAKey {
		tooLargeAKey[i] = 'L'
	}

	nbConnections := 2
	tick := time.AfterFunc(5*time.Second, func() {
		// We have to force close the connection, otherwise the connection hogging does not even
		// let us exit the func with t.Fatal (How? why?)
		kv.(*keyValue).DB.Close()
		t.Fatal("Test failed because SQL connections blocked by unrolled transactions")
	})
	kv.(*keyValue).DB.SetMaxOpenConns(nbConnections)
	for i := 0; i < nbConnections+1; i++ {
		b := kv.BeginBatch()
		// Making the transaction fail, to force a rollback
		// -> this whole test fails before we introduce the rollback in CommitBatch.
		b.Set(string(tooLargeAKey), "whatever")
		if err := kv.CommitBatch(b); err == nil {
			t.Fatal("wanted failed commit because too large a key")
		}
	}
	tick.Stop()
}