Merge pull request #1433 from nats-io/fix_tests

Fixed some tests
This commit is contained in:
Ivan Kozlovic
2020-05-29 17:56:42 -06:00
committed by GitHub
4 changed files with 66 additions and 88 deletions

View File

@@ -800,6 +800,7 @@ func TestFileStoreMeta(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer fs.Stop()
metafile := path.Join(storeDir, JetStreamMetaFile)
metasum := path.Join(storeDir, JetStreamMetaFileSum)
@@ -896,6 +897,7 @@ func TestFileStoreWriteAndReadSameBlock(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer fs.Stop()
subj, msg := "foo", []byte("Hello World!")
@@ -939,6 +941,7 @@ func TestFileStoreAndRetrieveMultiBlock(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer fs.Stop()
for i := uint64(1); i <= 20; i++ {
if _, _, _, err := fs.LoadMsg(i); err != nil {
@@ -962,6 +965,7 @@ func TestFileStoreCollapseDmap(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer fs.Stop()
for i := 0; i < 10; i++ {
fs.StoreMsg(subj, msg)
@@ -1169,6 +1173,7 @@ func TestFileStoreSnapshot(t *testing.T) {
if err != nil {
t.Fatalf("Error restoring from snapshot: %v", err)
}
defer fsr.Stop()
state := fs.State()
rstate := fsr.State()

View File

@@ -1491,7 +1491,7 @@ func TestLeafNodeTmpClients(t *testing.T) {
}
bo.LeafNode.Remotes = []*RemoteLeafOpts{{URLs: []*url.URL{u}}}
b := RunServer(bo)
defer a.Shutdown()
defer b.Shutdown()
checkLeafNodeConnected(t, b)
checkTmp(0)

View File

@@ -1979,7 +1979,6 @@ func TestMonitorRoutezRace(t *testing.T) {
srvBOpts := nextServerOpts(srvAOpts)
srvBOpts.Routes = RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", srvA.ClusterAddr().Port))
url := fmt.Sprintf("http://127.0.0.1:%d/", srvA.MonitorAddr().Port)
doneCh := make(chan struct{})
go func() {
defer func() {
@@ -1997,10 +1996,8 @@ func TestMonitorRoutezRace(t *testing.T) {
}()
done := false
for !done {
if resp, err := http.Get(url + "routez"); err != nil {
if _, err := srvA.Routez(nil); err != nil {
time.Sleep(10 * time.Millisecond)
} else {
resp.Body.Close()
}
select {
case <-doneCh:

View File

@@ -1394,23 +1394,21 @@ func TestConnectErrorReports(t *testing.T) {
s = RunServer(opts)
defer s.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(50 * routeConnectDelay)
s.Shutdown()
content, err := ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
checkContent := func(t *testing.T, txt string, attempt int, shouldBeThere bool) {
t.Helper()
present := bytes.Contains(content, []byte(fmt.Sprintf("%s (attempt %d)", txt, attempt)))
if shouldBeThere && !present {
t.Fatalf("Did not find expected log statement (%s) for attempt %d: %s", txt, attempt, content)
} else if !shouldBeThere && present {
t.Fatalf("Log statement (%s) for attempt %d should not be present: %s", txt, attempt, content)
}
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error {
content, err := ioutil.ReadFile(log)
if err != nil {
return fmt.Errorf("Error reading log file: %v", err)
}
present := bytes.Contains(content, []byte(fmt.Sprintf("%s (attempt %d)", txt, attempt)))
if shouldBeThere && !present {
return fmt.Errorf("Did not find expected log statement (%s) for attempt %d: %s", txt, attempt, content)
} else if !shouldBeThere && present {
return fmt.Errorf("Log statement (%s) for attempt %d should not be present: %s", txt, attempt, content)
}
return nil
})
}
type testConnect struct {
@@ -1433,6 +1431,7 @@ func TestConnectErrorReports(t *testing.T) {
})
}
s.Shutdown()
os.Remove(log)
// Now try with leaf nodes
@@ -1443,23 +1442,21 @@ func TestConnectErrorReports(t *testing.T) {
s = RunServer(opts)
defer s.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(50 * opts.LeafNode.ReconnectInterval)
s.Shutdown()
content, err = ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
checkLeafContent := func(t *testing.T, txt, host string, attempt int, shouldBeThere bool) {
t.Helper()
present := bytes.Contains(content, []byte(fmt.Sprintf("%s %q (attempt %d)", txt, host, attempt)))
if shouldBeThere && !present {
t.Fatalf("Did not find expected log statement (%s %q) for attempt %d: %s", txt, host, attempt, content)
} else if !shouldBeThere && present {
t.Fatalf("Log statement (%s %q) for attempt %d should not be present: %s", txt, host, attempt, content)
}
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error {
content, err := ioutil.ReadFile(log)
if err != nil {
return fmt.Errorf("Error reading log file: %v", err)
}
present := bytes.Contains(content, []byte(fmt.Sprintf("%s %q (attempt %d)", txt, host, attempt)))
if shouldBeThere && !present {
return fmt.Errorf("Did not find expected log statement (%s %q) for attempt %d: %s", txt, host, attempt, content)
} else if !shouldBeThere && present {
return fmt.Errorf("Log statement (%s %q) for attempt %d should not be present: %s", txt, host, attempt, content)
}
return nil
})
}
for _, test := range []testConnect{
@@ -1477,6 +1474,7 @@ func TestConnectErrorReports(t *testing.T) {
})
}
s.Shutdown()
os.Remove(log)
// Now try with gateways
@@ -1493,15 +1491,6 @@ func TestConnectErrorReports(t *testing.T) {
s = RunServer(opts)
defer s.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(50 * gatewayConnectDelay)
s.Shutdown()
content, err = ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
for _, test := range []testConnect{
{"gateway_attempt_1", 1, true},
{"gateway_attempt_2", 2, false},
@@ -1568,23 +1557,24 @@ func TestReconnectErrorReports(t *testing.T) {
// Now shutdown the server s connected to.
cs.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(DEFAULT_ROUTE_RECONNECT + 50*routeConnectDelay)
s.Shutdown()
content, err := ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
// Specifically for route test, wait at least reconnect interval before checking logs
time.Sleep(DEFAULT_ROUTE_RECONNECT)
checkContent := func(t *testing.T, txt string, attempt int, shouldBeThere bool) {
t.Helper()
present := bytes.Contains(content, []byte(fmt.Sprintf("%s (attempt %d)", txt, attempt)))
if shouldBeThere && !present {
t.Fatalf("Did not find expected log statement (%s) for attempt %d: %s", txt, attempt, content)
} else if !shouldBeThere && present {
t.Fatalf("Log statement (%s) for attempt %d should not be present: %s", txt, attempt, content)
}
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error {
content, err := ioutil.ReadFile(log)
if err != nil {
return fmt.Errorf("Error reading log file: %v", err)
}
present := bytes.Contains(content, []byte(fmt.Sprintf("%s (attempt %d)", txt, attempt)))
if shouldBeThere && !present {
return fmt.Errorf("Did not find expected log statement (%s) for attempt %d: %s", txt, attempt, content)
} else if !shouldBeThere && present {
return fmt.Errorf("Log statement (%s) for attempt %d should not be present: %s", txt, attempt, content)
}
return nil
})
}
type testConnect struct {
@@ -1607,6 +1597,7 @@ func TestReconnectErrorReports(t *testing.T) {
})
}
s.Shutdown()
os.Remove(log)
// Now try with leaf nodes
@@ -1625,33 +1616,26 @@ func TestReconnectErrorReports(t *testing.T) {
s = RunServer(opts)
defer s.Shutdown()
checkFor(t, 3*time.Second, 10*time.Millisecond, func() error {
if nln := s.NumLeafNodes(); nln != 1 {
return fmt.Errorf("Number of leaf nodes is %d", nln)
}
return nil
})
checkLeafNodeConnected(t, s)
// Now shutdown the server s is connected to
cs.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(50 * opts.LeafNode.ReconnectInterval)
s.Shutdown()
content, err = ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
checkLeafContent := func(t *testing.T, txt, host string, attempt int, shouldBeThere bool) {
t.Helper()
present := bytes.Contains(content, []byte(fmt.Sprintf("%s %q (attempt %d)", txt, host, attempt)))
if shouldBeThere && !present {
t.Fatalf("Did not find expected log statement (%s %q) for attempt %d: %s", txt, host, attempt, content)
} else if !shouldBeThere && present {
t.Fatalf("Log statement (%s %q) for attempt %d should not be present: %s", txt, host, attempt, content)
}
checkFor(t, 2*time.Second, 15*time.Millisecond, func() error {
content, err := ioutil.ReadFile(log)
if err != nil {
return fmt.Errorf("Error reading log file: %v", err)
}
present := bytes.Contains(content, []byte(fmt.Sprintf("%s %q (attempt %d)", txt, host, attempt)))
if shouldBeThere && !present {
return fmt.Errorf("Did not find expected log statement (%s %q) for attempt %d: %s", txt, host, attempt, content)
} else if !shouldBeThere && present {
return fmt.Errorf("Log statement (%s %q) for attempt %d should not be present: %s", txt, host, attempt, content)
}
return nil
})
}
for _, test := range []testConnect{
@@ -1669,6 +1653,7 @@ func TestReconnectErrorReports(t *testing.T) {
})
}
s.Shutdown()
os.Remove(log)
// Now try with gateways
@@ -1698,15 +1683,6 @@ func TestReconnectErrorReports(t *testing.T) {
// Now stop server s is connecting to
cs.Shutdown()
// Wait long enough for the number of recurring attempts to happen
time.Sleep(50 * gatewayConnectDelay)
s.Shutdown()
content, err = ioutil.ReadFile(log)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
connTxt := fmt.Sprintf("Connecting to explicit gateway \"B\" (127.0.0.1:%d) at 127.0.0.1:%d", remoteGWPort, remoteGWPort)
dbgConnTxt := fmt.Sprintf("[DBG] %s", connTxt)
infConnTxt := fmt.Sprintf("[INF] %s", connTxt)