@@ -120,41 +120,48 @@ describe("createDeploymentWithNextVersion", () => {
120120 async ( { prisma } ) => {
121121 const { project, environment } = await seedEnvironment ( prisma ) ;
122122
123- const concurrency = 4 ;
124- // maxRetries: 0 → no retry path. Concurrent racers all attempt the same
125- // version; one wins, the rest must surface the wrapped collision error
126- // (not a raw P2002) so Sentry can distinguish exhaustion from any other
127- // unique-constraint violation.
128- const settled = await Promise . allSettled (
129- Array . from ( { length : concurrency } , ( _ , i ) =>
130- createDeploymentWithNextVersion (
131- prisma ,
132- environment . id ,
133- ( nextVersion ) => ( {
123+ let collisionVersion = "" ;
124+ // Insert the winner after the version is read, before the candidate is
125+ // inserted, so exhaustion does not depend on concurrent query scheduling.
126+ const deployment = createDeploymentWithNextVersion (
127+ prisma ,
128+ environment . id ,
129+ async ( nextVersion ) => {
130+ collisionVersion = nextVersion ;
131+ await prisma . workerDeployment . create ( {
132+ data : {
133+ environmentId : environment . id ,
134134 projectId : project . id ,
135- friendlyId : `deployment_${ i } _${ nextVersion } ` ,
136- shortCode : `short_${ i } _${ nextVersion } ` ,
137- contentHash : `hash_${ i } ` ,
138- } ) ,
139- { maxRetries : 0 }
140- )
141- )
135+ version : nextVersion ,
136+ friendlyId : `winner_${ environment . id } ` ,
137+ shortCode : "winner" ,
138+ contentHash : "winner" ,
139+ } ,
140+ } ) ;
141+
142+ return {
143+ projectId : project . id ,
144+ friendlyId : `candidate_${ environment . id } ` ,
145+ shortCode : "candidate" ,
146+ contentHash : "candidate" ,
147+ } ;
148+ } ,
149+ { maxRetries : 0 }
142150 ) ;
143151
144- const fulfilled = settled . filter ( ( s ) => s . status === "fulfilled" ) ;
145- const rejected = settled . filter ( ( s ) : s is PromiseRejectedResult => s . status === "rejected" ) ;
146-
147- expect ( fulfilled . length ) . toBeGreaterThanOrEqual ( 1 ) ;
148- expect ( rejected . length ) . toBeGreaterThanOrEqual ( 1 ) ;
149-
150- for ( const r of rejected ) {
151- expect ( r . reason ) . toBeInstanceOf ( DeploymentVersionCollisionError ) ;
152- const err = r . reason as DeploymentVersionCollisionError ;
153- expect ( err . environmentId ) . toBe ( environment . id ) ;
154- expect ( err . attempts ) . toBe ( 1 ) ;
155- expect ( err . lastAttemptedVersion ) . toMatch ( / ^ \d { 8 } \. \d + $ / ) ;
156- expect ( ( err . cause as { code ?: string } ) . code ) . toBe ( "P2002" ) ;
157- }
152+ await expect ( deployment ) . rejects . toBeInstanceOf ( DeploymentVersionCollisionError ) ;
153+ await expect ( deployment ) . rejects . toMatchObject ( {
154+ environmentId : environment . id ,
155+ attempts : 1 ,
156+ lastAttemptedVersion : expect . stringMatching ( / ^ \d { 8 } \. \d + $ / ) ,
157+ cause : { code : "P2002" } ,
158+ } ) ;
159+ expect (
160+ await prisma . workerDeployment . findMany ( {
161+ where : { environmentId : environment . id } ,
162+ select : { friendlyId : true , version : true } ,
163+ } )
164+ ) . toEqual ( [ { friendlyId : `winner_${ environment . id } ` , version : collisionVersion } ] ) ;
158165 }
159166 ) ;
160167} ) ;
0 commit comments