github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/vm_concurrent_test.go (about)

     1  //go:build concurrent
     2  
     3  /*
     4   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"sync"
    11  
    12  	. "gopkg.in/check.v1"
    13  )
    14  
    15  // Test_VMRefreshConcurrent is meant to prove and show that structures across
    16  // go-vcloud-director are not thread-safe. Go tests must be run with -race flag
    17  // to capture race condition. It is also guarded by `concurrent` build tag and
    18  // is not run by default.
    19  //
    20  // Run with go test -race -tags "concurrent" -check.vv -check.f Test_VMRefreshConcurrent .
    21  func (vcd *TestVCD) Test_VMRefreshConcurrent(check *C) {
    22  	var waitgroup sync.WaitGroup
    23  
    24  	// Find VApp
    25  	if vcd.vapp.VApp == nil {
    26  		check.Skip("skipping test because no vApp is found")
    27  	}
    28  
    29  	vapp := vcd.findFirstVapp()
    30  	vmType, vmName := vcd.findFirstVm(vapp)
    31  	if vmName == "" {
    32  		check.Skip("skipping test because no VM is found")
    33  	}
    34  	vm := NewVM(&vcd.client.Client)
    35  	vm.VM = &vmType
    36  
    37  	for counter := 0; counter < 5; counter++ {
    38  		waitgroup.Add(1)
    39  		go func() {
    40  			_ = vm.Refresh()
    41  			waitgroup.Done()
    42  			// check.Assert(err, IsNil)
    43  		}()
    44  	}
    45  	waitgroup.Wait()
    46  
    47  }