Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance HostProvider to update host provider on retryStart occurrence #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feature(client retries connect to zk server): Add the hostProvider.Up…
…dateServerList function to update the host provider when retryStart occurs twice
  • Loading branch information
ChenHaoHu committed May 10, 2023
commit 48e045bdad0f5b33bf2a5a64e609dca6467b5b6d
68 changes: 68 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package zk

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -196,3 +198,69 @@ func TestNotifyWatches(t *testing.T) {
})
}
}

func TestConnLoop(t *testing.T) {

hostProvider := &DNSHostProvider{}

seq := 1
executedCount := 0
ipNum := 3

// simulate dns resolution to different ip addresses
hostProvider.lookupHost = func(string) ([]string, error) {
fmt.Println("Executed lookupHost,", executedCount)

executedCount++

if executedCount > 3 {
return nil, errors.New("lookupHost error")
}

ips := []string{}

for i := 0; i < ipNum; i++ {
seq = seq + 1
ips = append(ips, fmt.Sprintf("127.0.0.%v", seq))
}

return ips, nil
}

srvs := []string{"zk-test:2181"}

conn := &Conn{
dialer: net.DialTimeout,
hostProvider: hostProvider,
conn: nil,
state: StateDisconnected,
shouldQuit: make(chan struct{}),
connectTimeout: 1 * time.Second,
sendChan: make(chan *request, sendChanSize),
requests: make(map[int32]*request),
watchers: make(map[watchPathType][]chan Event),
passwd: emptyPassword,
logger: DefaultLogger,
logInfo: true, // default is true for backwards compatability
buf: make([]byte, bufferSize),
resendZkAuthFn: resendZkAuth,
configAddress: srvs,
}

if err := conn.hostProvider.Init(srvs); err != nil {
t.Fatal(err)
}

for {
if err := conn.connect(); err != nil {
// c.Close() was called
return
}
conn.hostProvider.Connected()

// mock conn disconnect
<-time.After(time.Second * 3)

fmt.Println("conn disconnect")
}
}