1package locking
2
3import (
4	"io/ioutil"
5	"os"
6	"testing"
7
8	"github.com/stretchr/testify/assert"
9)
10
11func TestLockCache(t *testing.T) {
12	var err error
13
14	tmpf, err := ioutil.TempFile("", "testCacheLock")
15	assert.Nil(t, err)
16	defer func() {
17		os.Remove(tmpf.Name())
18	}()
19	tmpf.Close()
20
21	cache, err := NewLockCache(tmpf.Name())
22	assert.Nil(t, err)
23
24	testLocks := []Lock{
25		Lock{Path: "folder/test1.dat", Id: "101"},
26		Lock{Path: "folder/test2.dat", Id: "102"},
27		Lock{Path: "root.dat", Id: "103"},
28	}
29
30	for _, l := range testLocks {
31		err = cache.Add(l)
32		assert.Nil(t, err)
33	}
34
35	locks := cache.Locks()
36	for _, l := range testLocks {
37		assert.Contains(t, locks, l)
38	}
39	assert.Equal(t, len(testLocks), len(locks))
40
41	err = cache.RemoveByPath("folder/test2.dat")
42	assert.Nil(t, err)
43
44	locks = cache.Locks()
45	// delete item 1 from test locls
46	testLocks = append(testLocks[:1], testLocks[2:]...)
47	for _, l := range testLocks {
48		assert.Contains(t, locks, l)
49	}
50	assert.Equal(t, len(testLocks), len(locks))
51
52	err = cache.RemoveById("101")
53	assert.Nil(t, err)
54
55	locks = cache.Locks()
56	testLocks = testLocks[1:]
57	for _, l := range testLocks {
58		assert.Contains(t, locks, l)
59	}
60	assert.Equal(t, len(testLocks), len(locks))
61}
62