Skip to content

Commit ee3fa4d

Browse files
authored
test: clihttp use https://example.com/ (#153)
* test: clihttp use https://example.com/ * test: add timeout arg for etcd * feat: add default timeout
1 parent a7b42b7 commit ee3fa4d

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

clihttp/client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ func TestClient_Do(t *testing.T) {
2121
}{
2222
{
2323
"normal",
24-
func() *http.Request { r, _ := http.NewRequest("GET", "https://baidu.com", nil); return r }(),
24+
func() *http.Request { r, _ := http.NewRequest("GET", "https://example.com/", nil); return r }(),
2525
[]Option{},
2626
},
2727
{
2828
"large request",
2929
func() *http.Request {
30-
r, _ := http.NewRequest("POST", "https://baidu.com", strings.NewReader(strings.Repeat("t", 10)))
30+
r, _ := http.NewRequest("POST", "https://example.com/", strings.NewReader(strings.Repeat("t", 10)))
3131
return r
3232
}(),
3333
[]Option{WithRequestLogThreshold(1)},
3434
},
3535
{
3636
"large response",
37-
func() *http.Request { r, _ := http.NewRequest("GET", "https://baidu.com", nil); return r }(),
37+
func() *http.Request { r, _ := http.NewRequest("GET", "https://example.com/", nil); return r }(),
3838
[]Option{WithResponseLogThreshold(1)},
3939
},
4040
}
@@ -58,7 +58,7 @@ func TestClient_race(t *testing.T) {
5858
for i := 0; i < 100; i++ {
5959
t.Run("", func(t *testing.T) {
6060
t.Parallel()
61-
r, _ := http.NewRequest("GET", "https://baidu.com", nil)
61+
r, _ := http.NewRequest("GET", "https://example.com/", nil)
6262
_, _ = client.Do(r)
6363
})
6464
}
@@ -70,7 +70,7 @@ func TestClient_context(t *testing.T) {
7070
span1, ctx := opentracing.StartSpanFromContextWithTracer(ctx, tracer, "span1")
7171
span1.SetBaggageItem("foo", "bar")
7272
span1.Finish()
73-
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://baidu.com", nil)
73+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://example.com/", nil)
7474

7575
client := NewClient(tracer)
7676
_, _ = client.Do(req)

clihttp/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88

99
func Example() {
1010
client := NewClient(opentracing.GlobalTracer())
11-
req, _ := http.NewRequest("GET", "https://baidu.com", nil)
11+
req, _ := http.NewRequest("GET", "https://example.com/", nil)
1212
_, _ = client.Do(req)
1313
}

leader/election_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func TestElection(t *testing.T) {
3030
var dispatcher = &events.SyncDispatcher{}
3131
var e1, e2 Election
3232

33-
client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs})
33+
client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs, DialTimeout: 2 * time.Second})
3434
assert.NoError(t, err)
35+
defer client.Close()
36+
3537
e1 = Election{
3638
dispatcher: dispatcher,
3739
status: &Status{isLeader: &atomic.Bool{}},

leader/leaderetcd/etcd_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"testing"
8+
"time"
89

910
"github.com/DoNewsCode/core/internal"
1011
"github.com/DoNewsCode/core/key"
@@ -23,7 +24,10 @@ func TestMain(m *testing.M) {
2324
}
2425

2526
func TestNewEtcdDriver(t *testing.T) {
26-
client, _ := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs})
27+
client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs, DialTimeout: 2 * time.Second})
28+
assert.NoError(t, err)
29+
defer client.Close()
30+
2731
e1 := NewEtcdDriver(client, key.New("test"))
2832
e2 := NewEtcdDriver(client, key.New("test"))
2933

otetcd/dependency.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func provideFactory(p factoryIn) (FactoryOut, func()) {
9292
co := clientv3.Config{
9393
Endpoints: conf.Endpoints,
9494
AutoSyncInterval: duration(conf.AutoSyncInterval),
95-
DialTimeout: duration(conf.DialTimeout),
95+
DialTimeout: duration(conf.dialTimeout()),
9696
DialKeepAliveTime: duration(conf.DialKeepAliveTime),
9797
DialKeepAliveTimeout: duration(conf.DialKeepAliveTimeout),
9898
MaxCallSendMsgSize: conf.MaxCallSendMsgSize,

otetcd/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/DoNewsCode/core/config"
77
"go.uber.org/zap"
88
"google.golang.org/grpc"
9+
"time"
910
)
1011

1112
// Option is a type that holds all of available etcd configurations.
@@ -70,3 +71,11 @@ type Option struct {
7071
// PermitWithoutStream when set will allow client to send keepalive pings to server without any active streams(RPCs).
7172
PermitWithoutStream bool `json:"permitWithoutStream" yaml:"permitWithoutStream"`
7273
}
74+
75+
// GetDialTimeout provide default timeout duration, avoid program blocking.
76+
func (o *Option) dialTimeout() config.Duration {
77+
if o.DialTimeout.IsZero() {
78+
return config.Duration{Duration: 10 * time.Second}
79+
}
80+
return o.DialTimeout
81+
}

0 commit comments

Comments
 (0)