-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlock_base.go
136 lines (117 loc) · 3.79 KB
/
lock_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"ChubbyGo/BaseServer"
_ "ChubbyGo/Config"
"ChubbyGo/Connect"
"fmt"
"log"
"time"
)
// 测试分为两个板块,基础锁定操作与延迟锁定
func main(){
clientConfigs := Connect.CreateClient()
err := clientConfigs.StartClient()
if err != nil {
log.Println(err.Error())
}
clientConfigs.SetUniqueFlake(uint64(0)) // 多次测试需要手动修改这个值
// 打开一个文件,获得句柄
ok, fd := clientConfigs.Open("/ls/ChubbyCell_lizhaolong")
if ok {
fmt.Printf("Get fd sucess, instanceSeq is %d\n", fd.InstanceSeq)
} else {
fmt.Printf("Error!\n")
}
filename := "text.txt"
// 在打开的文件夹下创建文件
ok, fileFd := clientConfigs.Create(fd, BaseServer.PermanentFile,filename)
if ok {
fmt.Printf("Create file(%s) sucess, instanceSeq is %d, checksum is %d.\n", filename,fileFd.InstanceSeq, fileFd.ChuckSum)
} else {
fmt.Printf("Create Error!\n")
}
// 删除句柄,注意句柄仅由create创建,delete删除
ok = clientConfigs.Delete(fileFd, BaseServer.Opdelete)
if ok {
fmt.Printf("Delete file(%s) sucess\n", filename)
} else {
fmt.Printf("Delete Error!\n")
}
// 第二次创建文件,返回的instanceSeq为1
ok, fileFd = clientConfigs.Create(fd, BaseServer.PermanentFile,filename)
if ok {
fmt.Printf("Create file(%s) sucess, instanceSeq is %d, checksum is %d.\n", filename, fileFd.InstanceSeq, fileFd.ChuckSum)
} else {
fmt.Printf("Create Error!\n")
}
// 对刚刚创建文件加锁
ok, token := clientConfigs.Acquire(fileFd, BaseServer.ReadLock, 0)
if ok {
fmt.Printf("Acquire (%s) sucess, Token is %d\n", filename, token)
} else {
fmt.Printf("Acquire Error!\n")
}
// 显然一个节点加了读锁以后再加有点蠢
ok , token = clientConfigs.Acquire(fileFd, BaseServer.ReadLock, 0)
if ok {
fmt.Printf("Acquire (%s) sucess, Token is %d\n", filename, token)
} else { // 显然加了写锁以后无法加读锁
fmt.Printf("ReadLock Error!\n")
}
// 删除文件的时候带上自己加锁的Token
ok = clientConfigs.Release(fileFd, token)
if ok {
fmt.Printf("release (%s) sucess.\n", filename)
} else {
fmt.Printf("Release Error!\n")
}
ok = clientConfigs.Release(fileFd, token)
if ok {
fmt.Printf("release (%s) sucess.\n", filename)
} else {
fmt.Printf("Release Error!\n")
}
ok, token = clientConfigs.Acquire(fileFd, BaseServer.WriteLock,1000)
if ok {
fmt.Printf("Acquire (%s) sucess, Token is %d\n", filename, token)
} else { // 显然加了写锁以后无法加读锁
fmt.Printf("WriteLock Error!\n")
}
// 超时以后使用此token去请求数据时会出现问题,TODO 但还没有实现带token的请求数据
// 先失败
ok, token = clientConfigs.Acquire(fileFd, BaseServer.WriteLock,0)
if ok {
fmt.Printf("Acquire (%s) sucess, Token is %d\n", filename, token)
} else { // 显然加了写锁以后无法加读锁
fmt.Printf("WriteLock Error!\n")
}
// 2000ms以后还能再次加锁成功,因为上一次的锁已经超时了
time.Sleep(2000 * time.Millisecond)
ok, token = clientConfigs.Acquire(fileFd, BaseServer.WriteLock,0)
if ok {
fmt.Printf("Acquire (%s) sucess, Token is %d\n", filename, token)
} else { // 显然加了写锁以后无法加读锁
fmt.Printf("WriteLock Error!\n")
}
ok = clientConfigs.Release(fileFd, token)
if ok {
fmt.Printf("release (%s) sucess.\n", filename)
} else {
fmt.Printf("Release Error!\n")
}
// 使用上面已经解锁的token 应该返回false
ok = clientConfigs.CheckToken(fileFd.PathName, token)
if ok {
fmt.Printf("CheckToken error! filename(%s)\n", fileFd.PathName)
} else {
fmt.Printf("CheckToken sucess!\n")
}
// 最后删除文件,方便测试lock_expand.go
ok = clientConfigs.Delete(fileFd, BaseServer.Opdelete)
if ok {
fmt.Printf("Delete file(%s) sucess\n", filename)
} else {
fmt.Printf("Delete Error!\n")
}
return
}