@@ -26,6 +26,27 @@ const organizationId = '1081635000895';
26
26
const orgName = 'organizations/' + organizationId ;
27
27
const pubsubTopic = 'projects/project-a-id/topics/notifications-sample-topic' ;
28
28
29
+ async function waitForConfig ( client , configId ) {
30
+ const maxRetries = 10 ;
31
+ const retryDelay = 1000 ; // 1 second
32
+ let retries = 0 ;
33
+
34
+ while ( retries < maxRetries ) {
35
+ try {
36
+ const name = client . organizationNotificationConfigPath ( organizationId , configId ) ;
37
+ const [ config ] = await client . getNotificationConfig ( { name} ) ;
38
+ if ( config ) return ;
39
+ } catch ( err ) {
40
+ // Ignore "not found" errors
41
+ if ( err . code !== 404 ) throw err ;
42
+ }
43
+ retries ++ ;
44
+ await new Promise ( resolve => setTimeout ( resolve , retryDelay ) ) ;
45
+ }
46
+
47
+ throw new Error ( `Timeout waiting for config ${ configId } to be available` ) ;
48
+ }
49
+
29
50
describe ( 'Client with Notifications' , async ( ) => {
30
51
const createConfig = 'notif-config-test-node-create' + uuidv1 ( ) ;
31
52
const deleteConfig = 'notif-config-test-node-delete' + uuidv1 ( ) ;
@@ -48,17 +69,24 @@ describe('Client with Notifications', async () => {
48
69
} ,
49
70
} ) ;
50
71
} catch ( err ) {
51
- console . error ( `Error creating config ${ configId } :` , err . message ) ;
72
+ if ( err . code === 400 ) {
73
+ console . error ( `Invalid input for config ${ configId } :` , err . message ) ;
74
+ } else if ( err . code === 503 ) {
75
+ console . error ( `Service unavailable when creating config ${ configId } :` , err . message ) ;
76
+ } else {
77
+ console . error ( `Unexpected error creating config ${ configId } :` , err . message ) ;
78
+ }
52
79
}
53
80
}
54
81
55
82
await createNotificationConfig ( deleteConfig ) ;
56
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
83
+ await waitForConfig ( client , deleteConfig ) ;
57
84
await createNotificationConfig ( getConfig ) ;
58
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
85
+ await waitForConfig ( client , getConfig ) ;
59
86
await createNotificationConfig ( listConfig ) ;
60
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
87
+ await waitForConfig ( client , listConfig ) ;
61
88
await createNotificationConfig ( updateConfig ) ;
89
+ await waitForConfig ( client , updateConfig ) ;
62
90
} ) ;
63
91
64
92
after ( async ( ) => {
@@ -71,16 +99,22 @@ describe('Client with Notifications', async () => {
71
99
) ;
72
100
await client . deleteNotificationConfig ( { name : name } ) ;
73
101
} catch ( err ) {
74
- console . error ( `Error deleting config ${ configId } :` , err . message ) ;
102
+ if ( err . code === 404 ) {
103
+ console . warn ( `Config ${ configId } not found during deletion:` , err . message ) ;
104
+ } else if ( err . code === 503 ) {
105
+ console . error ( `Service unavailable when deleting config ${ configId } :` , err . message ) ;
106
+ } else {
107
+ console . error ( `Unexpected error deleting config ${ configId } :` , err . message ) ;
108
+ }
75
109
}
76
110
}
77
111
78
112
await deleteNotificationConfig ( createConfig ) ;
79
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
113
+ await waitForConfig ( client , createConfig ) ;
80
114
await deleteNotificationConfig ( getConfig ) ;
81
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
115
+ await waitForConfig ( client , getConfig ) ;
82
116
await deleteNotificationConfig ( listConfig ) ;
83
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // 1-second delay
117
+ await waitForConfig ( client , listConfig ) ;
84
118
await deleteNotificationConfig ( updateConfig ) ;
85
119
} ) ;
86
120
0 commit comments