@@ -17,47 +17,6 @@ import (
1717// an app already exists but is in DELETING state.
1818func TestAppDoCreate_RetriesWhenAppIsDeleting (t * testing.T ) {
1919 server := testserver .New (t )
20-
21- createCallCount := 0
22- getCallCount := 0
23-
24- server .Handle ("POST" , "/api/2.0/apps" , func (req testserver.Request ) any {
25- createCallCount ++
26- if createCallCount == 1 {
27- return testserver.Response {
28- StatusCode : 409 ,
29- Body : map [string ]string {
30- "error_code" : "RESOURCE_ALREADY_EXISTS" ,
31- "message" : "An app with the same name already exists." ,
32- },
33- }
34- }
35- return apps.App {
36- Name : "test-app" ,
37- ComputeStatus : & apps.ComputeStatus {
38- State : apps .ComputeStateActive ,
39- },
40- }
41- })
42-
43- server .Handle ("GET" , "/api/2.0/apps/{name}" , func (req testserver.Request ) any {
44- getCallCount ++
45- if getCallCount == 1 {
46- return apps.App {
47- Name : req .Vars ["name" ],
48- ComputeStatus : & apps.ComputeStatus {
49- State : apps .ComputeStateDeleting ,
50- },
51- }
52- }
53- return apps.App {
54- Name : req .Vars ["name" ],
55- ComputeStatus : & apps.ComputeStatus {
56- State : apps .ComputeStateActive ,
57- },
58- }
59- })
60-
6120 testserver .AddDefaultHandlers (server )
6221
6322 client , err := databricks .NewWorkspaceClient (& databricks.Config {
@@ -66,60 +25,42 @@ func TestAppDoCreate_RetriesWhenAppIsDeleting(t *testing.T) {
6625 })
6726 require .NoError (t , err )
6827
69- r := (& ResourceApp {}).New (client )
7028 ctx := t .Context ()
29+
30+ // Create then delete an app to put it in DELETING state.
31+ // The testserver's DELETE is asynchronous: it sets DELETING rather than
32+ // removing immediately, so the retry create will find the app in that state.
33+ _ , err = client .Apps .Create (ctx , apps.CreateAppRequest {App : apps.App {Name : "test-app" }})
34+ require .NoError (t , err )
35+ _ , err = client .Apps .DeleteByName (ctx , "test-app" )
36+ require .NoError (t , err )
37+
38+ r := (& ResourceApp {}).New (client )
7139 name , _ , err := r .DoCreate (ctx , NewNopEngine (reflect .TypeFor [* AppState ]()), & AppState {App : apps.App {Name : "test-app" }})
7240
7341 require .NoError (t , err )
7442 assert .Equal (t , "test-app" , name )
75- assert .Equal (t , 2 , createCallCount , "expected Create to be called twice (1 retry)" )
76- assert .Equal (t , 2 , getCallCount , "expected Get to be called twice: once to check app state, once by waitForApp" )
7743}
7844
7945// TestAppDoCreate_RetriesWhenGetReturnsNotFound verifies that DoCreate retries
8046// when the app was just deleted between the create call and the get call.
8147func TestAppDoCreate_RetriesWhenGetReturnsNotFound (t * testing.T ) {
8248 server := testserver .New (t )
8349
84- createCallCount := 0
85- getCallCount := 0
86-
50+ // Simulate a race: the app existed when Create was called (returns 409) but
51+ // was deleted before the existence check (GET returns 404). The first POST
52+ // returns 409 without storing anything so the standard GET handler returns
53+ // 404 naturally, and the retry POST creates the app normally.
54+ rejectedOnce := false
8755 server .Handle ("POST" , "/api/2.0/apps" , func (req testserver.Request ) any {
88- createCallCount ++
89- if createCallCount == 1 {
56+ if ! rejectedOnce {
57+ rejectedOnce = true
9058 return testserver.Response {
9159 StatusCode : 409 ,
92- Body : map [string ]string {
93- "error_code" : "RESOURCE_ALREADY_EXISTS" ,
94- "message" : "An app with the same name already exists." ,
95- },
60+ Body : map [string ]string {"error_code" : "RESOURCE_ALREADY_EXISTS" , "message" : "An app with the same name already exists." },
9661 }
9762 }
98- return apps.App {
99- Name : "test-app" ,
100- ComputeStatus : & apps.ComputeStatus {
101- State : apps .ComputeStateActive ,
102- },
103- }
104- })
105-
106- server .Handle ("GET" , "/api/2.0/apps/{name}" , func (req testserver.Request ) any {
107- getCallCount ++
108- if getCallCount == 1 {
109- return testserver.Response {
110- StatusCode : 404 ,
111- Body : map [string ]string {
112- "error_code" : "RESOURCE_DOES_NOT_EXIST" ,
113- "message" : "App not found." ,
114- },
115- }
116- }
117- return apps.App {
118- Name : req .Vars ["name" ],
119- ComputeStatus : & apps.ComputeStatus {
120- State : apps .ComputeStateActive ,
121- },
122- }
63+ return req .Workspace .AppsUpsert (req , "" )
12364 })
12465
12566 testserver .AddDefaultHandlers (server )
@@ -136,8 +77,6 @@ func TestAppDoCreate_RetriesWhenGetReturnsNotFound(t *testing.T) {
13677
13778 require .NoError (t , err )
13879 assert .Equal (t , "test-app" , name )
139- assert .Equal (t , 2 , createCallCount , "expected Create to be called twice" )
140- assert .Equal (t , 2 , getCallCount , "expected Get to be called twice: once to check app state, once by waitForApp" )
14180}
14281
14382func TestAppDoUpdate_UpdateMaskHasAllFields (t * testing.T ) {
0 commit comments