@@ -20,6 +20,12 @@ namespace Microsoft.Azure.Cosmos.ChangeFeed.Tests
2020 [ TestCategory ( "ChangeFeed" ) ]
2121 public class DocumentServiceLeaseManagerCosmosTests
2222 {
23+ [ TestCleanup ]
24+ public void TestCleanup ( )
25+ {
26+ Environment . SetEnvironmentVariable ( ConfigurationManager . ChangeFeedLeaseIdAsPartitionKeyEnabled , null ) ;
27+ }
28+
2329 /// <summary>
2430 /// Verifies that the update lambda updates the owner.
2531 /// </summary>
@@ -153,6 +159,83 @@ public async Task CreatesPartitionKeyBasedLease(int factoryType)
153159 ValidateRequestOptionsFactory ( requestOptionsFactory , pkRangeBasedLease ) ;
154160 }
155161
162+ [ TestMethod ]
163+ public async Task CreatesPartitionKeyBasedLeaseWithDeterministicPartitionKeyByDefault ( )
164+ {
165+ PartitionKey ? capturedPartitionKey = null ;
166+ RequestOptionsFactory requestOptionsFactory = new PartitionedByPartitionKeyCollectionRequestOptionsFactory ( ) ;
167+ string continuation = Guid . NewGuid ( ) . ToString ( ) ;
168+ DocumentServiceLeaseStoreManagerOptions options = new DocumentServiceLeaseStoreManagerOptions
169+ {
170+ HostName = Guid . NewGuid ( ) . ToString ( )
171+ } ;
172+
173+ Documents . PartitionKeyRange partitionKeyRange = new Documents . PartitionKeyRange ( )
174+ {
175+ Id = "0" ,
176+ MinInclusive = "" ,
177+ MaxExclusive = "FF"
178+ } ;
179+
180+ Mock < ContainerInternal > mockedContainer = new Mock < ContainerInternal > ( ) ;
181+ mockedContainer . Setup ( c => c . CreateItemStreamAsync (
182+ It . IsAny < Stream > ( ) ,
183+ It . IsAny < PartitionKey > ( ) ,
184+ It . IsAny < ItemRequestOptions > ( ) ,
185+ It . IsAny < CancellationToken > ( ) ) ) . Callback ( ( Stream stream , PartitionKey partitionKey , ItemRequestOptions itemRequestOptions , CancellationToken token ) => capturedPartitionKey = partitionKey )
186+ . ReturnsAsync ( ( Stream stream , PartitionKey partitionKey , ItemRequestOptions itemRequestOptions , CancellationToken token ) => new ResponseMessage ( System . Net . HttpStatusCode . OK ) { Content = stream } ) ;
187+
188+ DocumentServiceLeaseManagerCosmos documentServiceLeaseManagerCosmos = new DocumentServiceLeaseManagerCosmos (
189+ Mock . Of < ContainerInternal > ( ) ,
190+ mockedContainer . Object ,
191+ Mock . Of < DocumentServiceLeaseUpdater > ( ) ,
192+ options ,
193+ requestOptionsFactory ) ;
194+
195+ DocumentServiceLease lease = await documentServiceLeaseManagerCosmos . CreateLeaseIfNotExistAsync ( partitionKeyRange , continuation ) ;
196+
197+ Assert . IsTrue ( capturedPartitionKey . HasValue ) ;
198+ Assert . AreEqual ( new PartitionKey ( lease . Id ) , capturedPartitionKey . Value ) ;
199+ Assert . AreEqual ( lease . Id , lease . PartitionKey ) ;
200+ }
201+
202+ [ TestMethod ]
203+ public async Task CreatesEPKBasedLeaseWithLegacyGuidPartitionKeyWhenOptedOut ( )
204+ {
205+ Environment . SetEnvironmentVariable ( ConfigurationManager . ChangeFeedLeaseIdAsPartitionKeyEnabled , "false" ) ;
206+
207+ PartitionKey ? capturedPartitionKey = null ;
208+ RequestOptionsFactory requestOptionsFactory = new PartitionedByPartitionKeyCollectionRequestOptionsFactory ( ) ;
209+ string continuation = Guid . NewGuid ( ) . ToString ( ) ;
210+ DocumentServiceLeaseStoreManagerOptions options = new DocumentServiceLeaseStoreManagerOptions
211+ {
212+ HostName = Guid . NewGuid ( ) . ToString ( )
213+ } ;
214+
215+ FeedRangeEpk feedRangeEpk = new FeedRangeEpk ( new Documents . Routing . Range < string > ( "AA" , "BB" , true , false ) ) ;
216+
217+ Mock < ContainerInternal > mockedContainer = new Mock < ContainerInternal > ( ) ;
218+ mockedContainer . Setup ( c => c . CreateItemStreamAsync (
219+ It . IsAny < Stream > ( ) ,
220+ It . IsAny < PartitionKey > ( ) ,
221+ It . IsAny < ItemRequestOptions > ( ) ,
222+ It . IsAny < CancellationToken > ( ) ) ) . Callback ( ( Stream stream , PartitionKey partitionKey , ItemRequestOptions itemRequestOptions , CancellationToken token ) => capturedPartitionKey = partitionKey )
223+ . ReturnsAsync ( ( Stream stream , PartitionKey partitionKey , ItemRequestOptions itemRequestOptions , CancellationToken token ) => new ResponseMessage ( System . Net . HttpStatusCode . OK ) { Content = stream } ) ;
224+
225+ DocumentServiceLeaseManagerCosmos documentServiceLeaseManagerCosmos = new DocumentServiceLeaseManagerCosmos (
226+ Mock . Of < ContainerInternal > ( ) ,
227+ mockedContainer . Object ,
228+ Mock . Of < DocumentServiceLeaseUpdater > ( ) ,
229+ options ,
230+ requestOptionsFactory ) ;
231+
232+ DocumentServiceLease lease = await documentServiceLeaseManagerCosmos . CreateLeaseIfNotExistAsync ( feedRangeEpk , continuation ) ;
233+
234+ Assert . IsTrue ( capturedPartitionKey . HasValue ) ;
235+ Assert . AreNotEqual ( new PartitionKey ( lease . Id ) , capturedPartitionKey . Value ) ;
236+ Assert . IsTrue ( Guid . TryParse ( lease . PartitionKey , out _ ) ) ;
237+ }
238+
156239 /// <summary>
157240 /// Verifies a Release sets the owner on null
158241 /// </summary>
@@ -628,11 +711,12 @@ private static void ValidateRequestOptionsFactory(RequestOptionsFactory requestO
628711 else if ( requestOptionsFactory is PartitionedByPartitionKeyCollectionRequestOptionsFactory )
629712 {
630713 Assert . IsNotNull ( lease . PartitionKey ) ;
714+ Assert . AreEqual ( lease . Id , lease . PartitionKey ) ;
631715 }
632716 else
633717 {
634718 throw new Exception ( $ "Unkown type mapping for FactoryType: { requestOptionsFactory . GetType ( ) } .") ;
635719 }
636720 }
637721 }
638- }
722+ }
0 commit comments