Пример #1
0
func lockBasic(kv kvdb.Kvdb, t *testing.T) {
	lockMethods := getLockMethods(kv)

	for _, lockMethod := range lockMethods {
		fmt.Println("lock")

		key := "locktest"
		kvPair, err := lockMethod(key)
		assert.NoError(t, err, "Unexpected error in lock")

		if kvPair == nil {
			return
		}

		err = kv.Unlock(kvPair)
		assert.NoError(t, err, "Unexpected error from Unlock")

		kvPair, err = lockMethod(key)
		assert.NoError(t, err, "Failed to lock after unlock")

		err = kv.Unlock(kvPair)
		assert.NoError(t, err, "Unexpected error from Unlock")
	}
}
Пример #2
0
func lock(kv kvdb.Kvdb, t *testing.T) {
	lockMethods := getLockMethods(kv)

	for _, lockMethod := range lockMethods {
		fmt.Println("lock")

		key := "locktest"
		kvPair, err := lockMethod(key)
		assert.NoError(t, err, "Unexpected error in lock")

		if kvPair == nil {
			return
		}

		// For consul unlock does not deal with the value part of the kvPair
		/*
			stash := *kvPair
			stash.Value = []byte("hoohah")
			fmt.Println("bad unlock")
			err = kv.Unlock(&stash)
			assert.Error(t, err, "Unlock should fail for bad KVPair")
		*/

		fmt.Println("unlock")
		err = kv.Unlock(kvPair)
		assert.NoError(t, err, "Unexpected error from Unlock")

		fmt.Println("relock")
		kvPair, err = lockMethod(key)
		assert.NoError(t, err, "Failed to lock after unlock")

		fmt.Println("reunlock")
		err = kv.Unlock(kvPair)
		assert.NoError(t, err, "Unexpected error from Unlock")

		fmt.Println("repeat lock once")
		kvPair, err = lockMethod(key)
		assert.NoError(t, err, "Failed to lock unlock")

		done := 0
		go func() {
			time.Sleep(time.Second * 10)
			done = 1
			err = kv.Unlock(kvPair)
			fmt.Println("repeat lock unlock once")
			assert.NoError(t, err, "Unexpected error from Unlock")
		}()
		fmt.Println("repeat lock lock twice")
		kvPair, err = lockMethod(key)
		assert.NoError(t, err, "Failed to lock")
		assert.Equal(t, done, 1, "Locked before unlock")
		fmt.Println("repeat lock unlock twice")
		err = kv.Unlock(kvPair)
		assert.NoError(t, err, "Unexpected error from Unlock")

		for done == 0 {
			time.Sleep(time.Second)
		}

		key = "doubleLock"
		kvPair, err = lockMethod(key)
		assert.NoError(t, err, "Unexpected error in lock")
		go func() {
			time.Sleep(30 * time.Second)
			err = kv.Unlock(kvPair)
			assert.NoError(t, err, "Unexpected error from Unlock")
		}()
		kvPair2, err := lockMethod(key)
		assert.NoError(t, err, "Double lock")
		err = kv.Unlock(kvPair2)
		assert.NoError(t, err, "Unexpected error from Unlock")

	}
}