public async Task BasicCriteriaTest() { using (var store = GetDocumentStore()) using (var subscriptionManager = new DocumentSubscriptions(store)) { await CreateDocuments(store, 1); var lastEtag = store.GetLastWrittenEtag() ?? 0; await CreateDocuments(store, 5); var subscriptionCriteria = new SubscriptionCriteria { Collection = "Things", FilterJavaScript = " return this.Name == 'ThingNo3'", }; var subsId = subscriptionManager.Create(subscriptionCriteria, lastEtag); using (var subscription = subscriptionManager.Open <Thing>(new SubscriptionConnectionOptions() { SubscriptionId = subsId })) { var list = new BlockingCollection <Thing>(); subscription.Subscribe(x => { list.Add(x); }); await subscription.StartAsync(); Thing thing; Assert.True(list.TryTake(out thing, 5000)); Assert.Equal("ThingNo3", thing.Name); Assert.False(list.TryTake(out thing, 50)); } } }
public async Task BasicCriteriaTest(bool useSsl) { string dbName = GetDatabaseName(); X509Certificate2 clientCertificate = null; X509Certificate2 adminCertificate = null; if (useSsl) { var serverCertPath = SetupServerAuthentication(); adminCertificate = AskServerForClientCertificate(serverCertPath, new Dictionary <string, DatabaseAccess>(), SecurityClearance.ClusterAdmin); clientCertificate = AskServerForClientCertificate(serverCertPath, new Dictionary <string, DatabaseAccess> { [dbName] = DatabaseAccess.ReadWrite }); } using (var store = GetDocumentStore(new Options { AdminCertificate = adminCertificate, ClientCertificate = clientCertificate, ModifyDatabaseName = s => dbName })) { using (var subscriptionManager = new DocumentSubscriptions(store)) { await CreateDocuments(store, 1); var lastChangeVector = (await store.Admin.SendAsync(new GetStatisticsOperation())).DatabaseChangeVector; await CreateDocuments(store, 5); var subscriptionCreationParams = new SubscriptionCreationOptions() { Query = "from Things where Name = 'ThingNo3'", ChangeVector = lastChangeVector }; var subsId = subscriptionManager.Create(subscriptionCreationParams); using (var subscription = subscriptionManager.Open <Thing>(new SubscriptionConnectionOptions(subsId))) { var list = new BlockingCollection <Thing>(); GC.KeepAlive(subscription.Run(x => { foreach (var item in x.Items) { list.Add(item.Result); } })); Thing thing; Assert.True(list.TryTake(out thing, 5000)); Assert.Equal("ThingNo3", thing.Name); Assert.False(list.TryTake(out thing, 50)); } } } }
public async Task CriteriaScriptWithTransformation(bool useSsl) { string dbName = GetDatabaseName(); X509Certificate2 clientCertificate = null; X509Certificate2 adminCertificate = null; if (useSsl) { var serverCertPath = SetupServerAuthentication(); adminCertificate = AskServerForClientCertificate(serverCertPath, new Dictionary <string, DatabaseAccess>(), SecurityClearance.ClusterAdmin); clientCertificate = AskServerForClientCertificate(serverCertPath, new Dictionary <string, DatabaseAccess> { [dbName] = DatabaseAccess.ReadWrite, }); } using (var store = GetDocumentStore(new Options { AdminCertificate = adminCertificate, ClientCertificate = clientCertificate, ModifyDatabaseName = s => dbName })) { using (var subscriptionManager = new DocumentSubscriptions(store)) { await CreateDocuments(store, 1); var lastChangeVector = (await store.Admin.SendAsync(new GetStatisticsOperation())).DatabaseChangeVector; await CreateDocuments(store, 6); var subscriptionCreationParams = new SubscriptionCreationOptions() { Query = @" declare function project(d) { var namSuffix = parseInt(d.Name.replace('ThingNo', '')); if (namSuffix <= 2){ return false; } else if (namSuffix == 3){ return null; } else if (namSuffix == 4){ return d; } return {Name: 'foo', OtherDoc:load('things/6-A')} } from Things as d select project(d) ", ChangeVector = lastChangeVector }; var subsId = subscriptionManager.Create(subscriptionCreationParams); using (var subscription = subscriptionManager.Open <BlittableJsonReaderObject>(new SubscriptionConnectionOptions(subsId))) { using (store.GetRequestExecutor().ContextPool.AllocateOperationContext(out JsonOperationContext context)) { var list = new BlockingCollection <BlittableJsonReaderObject>(); GC.KeepAlive(subscription.Run(x => { foreach (var item in x.Items) { list.Add(context.ReadObject(item.Result, "test")); } })); BlittableJsonReaderObject thing; Assert.True(list.TryTake(out thing, 5000)); dynamic dynamicThing = new DynamicBlittableJson(thing); Assert.Equal("ThingNo4", dynamicThing.Name); Assert.True(list.TryTake(out thing, 5000)); dynamicThing = new DynamicBlittableJson(thing); Assert.Equal("foo", dynamicThing.Name); Assert.Equal("ThingNo4", dynamicThing.OtherDoc.Name); Assert.False(list.TryTake(out thing, 50)); } } } } }