@@ -8,8 +8,10 @@ package fwprovider_test
88
99import (
1010 "context"
11+ "sync"
1112 "testing"
1213
14+ "github.com/stretchr/testify/assert"
1315 "github.com/stretchr/testify/require"
1416
1517 "github.com/bpg/terraform-provider-proxmox/fwprovider/test"
@@ -22,7 +24,10 @@ import (
2224func TestIDGenerator_Sequence (t * testing.T ) {
2325 t .Parallel ()
2426
25- const numIDs = 10
27+ const (
28+ numIDs = 10
29+ numBusyIDs = 30
30+ )
2631
2732 if utils .GetAnyStringEnv ("TF_ACC" ) == "" {
2833 t .Skip ("Acceptance tests are disabled" )
@@ -36,42 +41,73 @@ func TestIDGenerator_Sequence(t *testing.T) {
3641 firstID , err := gen .NextID (ctx )
3742 require .NoError (t , err )
3843
39- busyID := firstID + 5
44+ firstBusyID := firstID + 5
4045
41- _ , err = te .ClusterClient ().GetNextID (ctx , ptr .Ptr (busyID ))
42- require .NoError (t , err , "the VM ID %d should be available" , busyID )
46+ _ , err = te .ClusterClient ().GetNextID (ctx , ptr .Ptr (firstBusyID ))
47+ require .NoError (t , err , "the VM ID %d should be available" , firstBusyID )
4348
44- err = te .NodeClient ().VM (0 ).CreateVM (ctx , & vms.CreateRequestBody {VMID : busyID })
45- require .NoError (t , err , "failed to create VM %d" , busyID )
49+ for i := range numBusyIDs {
50+ busyID := firstBusyID + i
51+ err = te .NodeClient ().VM (0 ).CreateVM (ctx , & vms.CreateRequestBody {VMID : busyID })
52+ require .NoError (t , err , "failed to create VM %d" , busyID )
53+ }
4654
4755 t .Cleanup (func () {
48- err = te .NodeClient ().VM (busyID ).DeleteVM (ctx )
49- require .NoError (t , err , "failed to delete VM %d" , busyID )
56+ var wg sync.WaitGroup
57+
58+ for i := range numBusyIDs {
59+ wg .Add (1 )
60+
61+ go func () {
62+ defer wg .Done ()
63+
64+ busyID := firstBusyID + i
65+ err = te .NodeClient ().VM (busyID ).DeleteVM (ctx )
66+ assert .NoError (t , err , "failed to delete VM %d" , busyID )
67+ }()
68+ }
69+
70+ wg .Wait ()
5071 })
5172
5273 ids := make ([]int , numIDs )
5374
5475 t .Cleanup (func () {
76+ var wg sync.WaitGroup
5577 for _ , id := range ids {
56- if id > 100 {
57- _ = te .NodeClient ().VM (id ).DeleteVM (ctx ) //nolint:errcheck
58- }
78+ wg .Add (1 )
79+
80+ go func () {
81+ defer wg .Done ()
82+
83+ if id > 100 {
84+ _ = te .NodeClient ().VM (id ).DeleteVM (ctx ) //nolint:errcheck
85+ }
86+ }()
5987 }
88+
89+ wg .Wait ()
6090 })
6191
62- prevID := firstID
92+ var wg sync. WaitGroup
6393
6494 for i := range numIDs {
65- id , err := gen .NextID (ctx )
66- require .NoError (t , err )
67- err = te .NodeClient ().VM (0 ).CreateVM (ctx , & vms.CreateRequestBody {VMID : id })
68- ids [i ] = id
95+ wg .Add (1 )
6996
70- require . NoError ( t , err )
71- require . Greater ( t , id , prevID , "the generated ID should be greater than the previous one" )
97+ go func () {
98+ defer wg . Done ( )
7299
73- prevID = id
100+ id , err := gen .NextID (ctx )
101+ if err == nil {
102+ err = te .NodeClient ().VM (0 ).CreateVM (ctx , & vms.CreateRequestBody {VMID : id })
103+ ids [i ] = id
104+ }
105+
106+ assert .NoError (t , err )
107+ }()
74108 }
109+
110+ wg .Wait ()
75111}
76112
77113func TestIDGenerator_Random (t * testing.T ) {
0 commit comments