@@ -2,10 +2,13 @@ package test
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
6
+ mik "go-learn/src/mk"
7
+ "go.etcd.io/etcd/client/v3/concurrency"
8
+ "reflect"
7
9
"testing"
8
10
"time"
11
+ "unsafe"
9
12
10
13
"github.com/simonalong/gole/util"
11
14
clientv3 "go.etcd.io/etcd/client/v3"
@@ -22,7 +25,7 @@ var Ctx = context.Background()
22
25
func init () {
23
26
// 客户端配置
24
27
config = clientv3.Config {
25
- Endpoints : []string {"10.30.30.78:22379 " },
28
+ Endpoints : []string {"10.30.30.78:52379 " },
26
29
DialTimeout : 5 * time .Second ,
27
30
Username : "root" ,
28
31
Password : "ZljIsysc0re123" ,
@@ -36,24 +39,24 @@ func init() {
36
39
}
37
40
38
41
func TestEtcd1 (t * testing.T ) {
39
- authRsp , _ := etcdClient .AuthStatus (Ctx )
40
- fmt .Println (authRsp )
42
+ // authRsp, _ := etcdClient.AuthStatus(Ctx)
43
+ // fmt.Println(authRsp)
41
44
42
- // res, err := etcdClient.Put(Ctx, "key4", "v1")
43
- // if err != nil {
44
- // fmt.Println(err)
45
- // return
46
- // }
47
- //
48
- // fmt.Println(res)
49
- //
50
- // res1, err := etcdClient.Get(Ctx, "key4")
51
- // if err != nil {
52
- // fmt.Println(err)
53
- // return
54
- // }
55
- //
56
- // fmt.Println(string(res1.Kvs[0].Value))
45
+ res , err := etcdClient .Put (Ctx , "key4" , "v1" )
46
+ if err != nil {
47
+ fmt .Println (err )
48
+ return
49
+ }
50
+
51
+ fmt .Println (res )
52
+
53
+ res1 , err := etcdClient .Get (Ctx , "key4" )
54
+ if err != nil {
55
+ fmt .Println (err )
56
+ return
57
+ }
58
+
59
+ fmt .Println (string (res1 .Kvs [0 ].Value ))
57
60
}
58
61
59
62
func TestKeys (t * testing.T ) {
@@ -173,13 +176,173 @@ func write(index int) {
173
176
174
177
func TestName (t * testing.T ) {
175
178
176
- dataMap := map [string ]string {}
177
- dataMap ["a" ] = "a"
178
- dataMap ["b" ] = "b"
179
+ //dataMap := map[string]string{}
180
+ //dataMap["a"] = "a"
181
+ //dataMap["b"] = "b"
182
+ //
183
+ //bytes, err := json.Marshal(12)
184
+ //if err != nil {
185
+ // fmt.Printf("%v", err.Error())
186
+ //}
187
+ //fmt.Println(string(bytes))
188
+
189
+ //fmt.Println(*flag.String("a", "b","c"))
190
+ }
191
+
192
+ func TestLock1 (t * testing.T ) {
193
+
194
+ //为锁生成session
195
+ s1 , err := concurrency .NewSession (etcdClient , concurrency .WithTTL (5 ))
196
+ if err != nil {
197
+ t .Log (err .Error ())
198
+ return
199
+ }
200
+ defer s1 .Close ()
201
+
202
+ ctx := context .Background ()
203
+ locker := concurrency .NewMutex (s1 , "test/name" )
204
+ fmt .Println ("acquiring lock" )
205
+ if err := locker .TryLock (ctx ); err != nil {
206
+ fmt .Println ("尝试失败" , err .Error ())
207
+ return
208
+ }
209
+
210
+ // 请求锁
211
+
212
+ fmt .Println ("acquired lock" )
213
+
214
+ time .Sleep (time .Second * 10 )
179
215
180
- bytes , err := json .Marshal (12 )
216
+ //time.Sleep(time.Duration(rand.Intn(30))*time.Second)
217
+ locker .Unlock (ctx ) //释放锁
218
+ fmt .Println ("released lock" )
219
+ }
220
+
221
+ func TestLock2 (t * testing.T ) {
222
+
223
+ //为锁生成session
224
+ s1 , err := concurrency .NewSession (etcdClient , concurrency .WithTTL (5 ))
181
225
if err != nil {
182
- fmt .Printf ("%v" , err .Error ())
226
+ t .Log (err .Error ())
227
+ return
183
228
}
184
- fmt .Println (string (bytes ))
229
+ defer s1 .Close ()
230
+ fmt .Println ("acquiring lock" )
231
+ ctx := context .Background ()
232
+ locker := concurrency .NewMutex (s1 , "test/name" )
233
+ if err := locker .TryLock (ctx ); err != nil {
234
+ fmt .Println ("尝试失败" , err .Error ())
235
+ return
236
+ }
237
+
238
+ // 请求锁
239
+ fmt .Println ("acquired lock" )
240
+
241
+ time .Sleep (time .Second * 10 )
242
+
243
+ //time.Sleep(time.Duration(rand.Intn(30))*time.Second)
244
+ locker .Unlock (ctx ) //释放锁
245
+ fmt .Println ("released lock" )
246
+ }
247
+
248
+ type Demo struct {
249
+ private string
250
+ youCannotSeeMe int
251
+ Trick bool
252
+ }
253
+
254
+ func TestPrivate (t * testing.T ) {
255
+ //d := Demo{private: "hahaha", youCannotSeeMe: 110, Trick: true}
256
+
257
+ //type Header struct {
258
+ // NotPrivate string
259
+ // YouCanSeeMe int
260
+ //}
261
+ //
262
+ //h := *(*Header)(uintptr(unsafe.Pointer(&d)))
263
+ //h.YouCanSeeMe = 32
264
+ //
265
+ //fmt.Printf("%+v", h)
266
+ //fmt.Printf("%+v", d)
267
+
268
+ //priV := reflect.ValueOf(d).FieldByName("private")
269
+ //priV = reflect.NewAt(priV.Type(), unsafe.Pointer(priV.UnsafeAddr())).Elem()
270
+ //v := priV.Interface()
271
+ //pvv := v.(string)
272
+ //pvv = "sdfsdf"
273
+ }
274
+
275
+ type TestPointer struct {
276
+ A int
277
+ b int // 私有变量
278
+ }
279
+
280
+ func (T * TestPointer ) OouPut () {
281
+ fmt .Println ("TestPointer OouPut:" , T .A , T .b )
282
+ }
283
+
284
+ func TestPrivate2 (t * testing.T ) {
285
+ T := TestPointer {A : 1 }
286
+ pb := (* int )(unsafe .Pointer (uintptr (unsafe .Pointer (& T )) + 8 ))
287
+ /*
288
+ Tmp := uintptr(unsafe.Pointer(&T)) + 8)
289
+ pb := (*int)(unsafe.Pointer(Tmp)
290
+ 千万不能出现这种用临时变量中转一下的情况。因为GC可能因为优化内存碎片的原因移动了这个对象。只保留了指针的地址是没有意义的。
291
+ */
292
+ * pb = 2
293
+ T .OouPut () //1 2
294
+ }
295
+
296
+ //// Index 获取首页信息
297
+ //func (receiver *Home) Index(req *controllers.HomeRequest, ctx *gin.Context) (*controllers.HomeResponse, error) {
298
+ // value := reflect.ValueOf(ctx).Elem()
299
+ //
300
+ // engine := value.FieldByName("engine")
301
+ // // rf can't be read or set.
302
+ // engine = reflect.NewAt(engine.Type(), unsafe.Pointer(engine.UnsafeAddr())).Elem()
303
+ // v := engine.Interface()
304
+ // context := v.(*gin.Engine)
305
+ // help.Dump(context)
306
+ //
307
+ // return &controllers.HomeResponse{}, nil
308
+ //}
309
+
310
+ type TestStruct struct {
311
+ mik.TestStruct33
312
+ testField int
313
+ }
314
+
315
+ func (pt TestStruct ) Namess () []string {
316
+ return pt .TestStruct33 .Namess ()
317
+ }
318
+
319
+ func TestPrivate3 (t * testing.T ) {
320
+ //var s = TestStruct{testField: 100, name: []string{"z", "haha"}}
321
+ //
322
+ //// 获取旧数据
323
+ //ov := GetPrivateFieldValue(reflect.ValueOf(&s), "name")
324
+ //// 100
325
+ //fmt.Println(ov)
326
+ //
327
+ //ovV := ov.([]string)
328
+ //newDatas := append(ovV, "kkk")
329
+ //// 修改为新数据
330
+ //var data []string
331
+ //data = newDatas
332
+ //SetFieldPrivateValue(reflect.ValueOf(&s), "name", reflect.ValueOf(&data))
333
+ //// 21
334
+ //fmt.Println(s)
335
+ }
336
+
337
+ // 获取对象的私有属性
338
+ func GetPrivateFieldValue (objPtrValue reflect.Value , fieldName string ) interface {} {
339
+ fieldValue := objPtrValue .Elem ().FieldByName (fieldName )
340
+ return reflect .NewAt (fieldValue .Type (), unsafe .Pointer (fieldValue .UnsafeAddr ())).Elem ().Interface ()
341
+ }
342
+
343
+ // 给对象的属性设置值
344
+ func SetFieldPrivateValue (objPtrValue reflect.Value , fieldName string , fieldNewValue reflect.Value ) {
345
+ fieldValue := objPtrValue .Elem ().FieldByName (fieldName )
346
+ fieldValue = reflect .NewAt (fieldValue .Type (), unsafe .Pointer (fieldValue .UnsafeAddr ())).Elem ()
347
+ fieldValue .Set (fieldNewValue .Elem ())
185
348
}
0 commit comments