2019-01-23 19:18:17 +00:00
|
|
|
package svalue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-24 06:45:59 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
2019-01-23 19:18:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
)
|
|
|
|
|
2019-01-24 06:45:59 +00:00
|
|
|
type EtcdGet struct {
|
2019-01-23 19:18:17 +00:00
|
|
|
Value
|
|
|
|
}
|
|
|
|
|
2019-01-24 06:45:59 +00:00
|
|
|
func (c *EtcdGet) TakeValue() interface{} {
|
|
|
|
// 换成redis原理一样
|
2019-01-23 19:18:17 +00:00
|
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: []string{"http://10.10.0.1:2279"},
|
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
})
|
|
|
|
resp, err := cli.Get(context.Background(), "phone", clientv3.WithPrefix())
|
|
|
|
must(err)
|
|
|
|
return string(resp.Kvs[0].Value)[0:20]
|
|
|
|
}
|
|
|
|
|
2019-01-24 06:45:59 +00:00
|
|
|
func (c *EtcdGet) UpdateInterval() time.Duration {
|
|
|
|
return time.Second / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithVPN(t *testing.T) {
|
|
|
|
ec := EtcdGet{}
|
|
|
|
StartSynchronize(&ec)
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
must(err)
|
|
|
|
|
|
|
|
fitThis := false
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
if iface.Name == "tun0" {
|
|
|
|
fitThis = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fitThis {
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
v := ec.GetValue()
|
|
|
|
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case string:
|
|
|
|
t.Log(vt)
|
|
|
|
default:
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 1 {
|
|
|
|
ec.CloseSync()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Log("pass this test", t.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EtcdWatch 方法的实现, 先忽略
|
|
|
|
type EtcdWatch struct {
|
|
|
|
Value
|
2019-01-23 19:18:17 +00:00
|
|
|
}
|