72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
package svalue
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
)
|
|
|
|
type EtcdGet struct {
|
|
Value
|
|
}
|
|
|
|
func (c *EtcdGet) TakeValue() interface{} {
|
|
// 换成redis原理一样
|
|
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]
|
|
}
|
|
|
|
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
|
|
}
|