Skip to content

Commit

Permalink
Fix the race in the unit test
Browse files Browse the repository at this point in the history
After the backing image manager initialization, the test suite can
not modify the fields used in the manager monitor goroutines.

Signed-off-by: Shuo Wu <shuo.wu@suse.com>
  • Loading branch information
shuo-wu authored and innobead committed Apr 22, 2021
1 parent f65e3ae commit 87eb606
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ func (s *TestSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
}

s.m, err = NewManager("", "/var/lib/longhorn", testDiskPath, "30001-31000", make(chan error))
s.shutdownCh = make(chan error)
s.m, err = NewManager("bim-test-disk-cfg", "/var/lib/longhorn", testDiskPath, "30001-31000", s.shutdownCh)
if err != nil {
c.Assert(os.IsExist(err), Equals, true)
}
s.m.DownloaderFactory = &MockDownloaderFactory{}
s.shutdownCh = make(chan error)
s.m.diskPathInContainer = testDiskPath
s.m.diskUUID = "bim-test-disk-cfg"
s.m.Sender = MockSender
}

Expand Down Expand Up @@ -186,16 +184,16 @@ func (s *TestSuite) TestSingleBackingImageCRUD(c *C) {
// Each iteration takes 5 seconds.
count := 10
for i := 0; i < count; i++ {
bm := NewBackingImage(name, url, uuid, "/var/lib/longhorn", s.getTestDiskPath(c), mockDownloaderFactory.NewDownloader(), make(chan interface{}, 100))
bi := NewBackingImage(name, url, uuid, "/var/lib/longhorn", s.getTestDiskPath(c), mockDownloaderFactory.NewDownloader(), make(chan interface{}, 100))
var err error

err = bm.Delete()
err = bi.Delete()
c.Assert(err, IsNil)

if i%2 != 0 {
_, err = bm.Pull()
_, err = bi.Pull()
} else {
_, err = bm.Receive(MockDownloadSize, "SenderAddress", func(portCount int32) (int32, int32, error) {
_, err = bi.Receive(MockDownloadSize, "SenderAddress", func(portCount int32) (int32, int32, error) {
return 0, 0, nil
}, func(start, end int32) error {
return nil
Expand All @@ -205,7 +203,7 @@ func (s *TestSuite) TestSingleBackingImageCRUD(c *C) {

downloaded := false
for j := 0; j < RetryCount; j++ {
getResp := bm.Get()
getResp := bi.Get()
if getResp.Status.State == types.DownloadStateDownloaded {
downloaded = true
break
Expand All @@ -214,7 +212,7 @@ func (s *TestSuite) TestSingleBackingImageCRUD(c *C) {
}
c.Assert(downloaded, Equals, true)

err = bm.Delete()
err = bi.Delete()
c.Assert(err, IsNil)
}
}
Expand All @@ -228,9 +226,9 @@ func (s *TestSuite) TestBackingImageSimultaneousDownloadingAndCancellation(c *C)

count := 100
for i := 0; i < count; i++ {
bm := NewBackingImage(name, url, uuid, "/var/lib/longhorn", s.getTestDiskPath(c), mockDownloaderFactory.NewDownloader(), make(chan interface{}, 100))
bi := NewBackingImage(name, url, uuid, "/var/lib/longhorn", s.getTestDiskPath(c), mockDownloaderFactory.NewDownloader(), make(chan interface{}, 100))

err := bm.Delete()
err := bi.Delete()
c.Assert(err, IsNil)

// Start to do Pulling and Receiving simultaneously, which is impossible ideally.
Expand All @@ -240,11 +238,11 @@ func (s *TestSuite) TestBackingImageSimultaneousDownloadingAndCancellation(c *C)
wg.Add(2)
go func() {
defer wg.Done()
_, errPull = bm.Pull()
_, errPull = bi.Pull()
}()
go func() {
defer wg.Done()
_, errSync = bm.Receive(MockDownloadSize, "SenderAddress", func(portCount int32) (int32, int32, error) {
_, errSync = bi.Receive(MockDownloadSize, "SenderAddress", func(portCount int32) (int32, int32, error) {
return 0, 0, nil
}, func(start, end int32) error {
return nil
Expand All @@ -255,17 +253,17 @@ func (s *TestSuite) TestBackingImageSimultaneousDownloadingAndCancellation(c *C)

isDownloading := false
for j := 0; j < 5; j++ {
getResp := bm.Get()
getResp := bi.Get()
if getResp.Status.State == types.DownloadStateDownloading && getResp.Status.DownloadProgress > 0 {
isDownloading = true
break
}
time.Sleep(RetryInterval)
}
c.Assert(isDownloading, Equals, true)
err = bm.Delete()
err = bi.Delete()
c.Assert(err, IsNil)
getResp := bm.Get()
getResp := bi.Get()
c.Assert(getResp.Status.State, Equals, string(StateFailed))
}
}

0 comments on commit 87eb606

Please sign in to comment.