예제 #1
0
        public void TestReadPurgeResponseJson()
        {
            string        json = ReadDataFile("PurgeResponse.json");
            PurgeResponse pr   = new PurgeResponse(json, false);

            Assert.True(pr.Success);
            Assert.Equal(5ul, pr.Purged);
        }
예제 #2
0
        public void Aggregate_GivenMultipleResults_ThenPrioritise(PurgeResult[] mockResults, PurgeResult expectedResult)
        {
            var subjects = mockResults.Select(r => new PurgeResponse(r, null, null, null)).ToArray();

            var result = PurgeResponse.Aggregate(subjects);

            Assert.AreEqual(expectedResult, result.Result);
        }
예제 #3
0
        public async Task <PurgeResponse> PurgeAllAsync(PurgeAllRequest request)
        {
            var purgeTasks = _cdnApis.Where(c => c.IsEnabled())
                             .Select(cdn => cdn.PurgeAllAsync(request));

            var results = await Task.WhenAll(purgeTasks);

            return(PurgeResponse.Aggregate(results));
        }
예제 #4
0
        public void TestPurgeStreamAndOptions()
        {
            Context.RunInJsServer(c =>
            {
                Assert.Throws <ArgumentException>(() => PurgeOptions.Builder().WithKeep(1).WithSequence(1).Build());

                IJetStreamManagement jsm = c.CreateJetStreamManagementContext();
                Assert.Throws <NATSJetStreamException>(() => jsm.PurgeStream(STREAM));

                CreateMemoryStream(c, STREAM, Subject(1), Subject(2));

                StreamInfo si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(0u, si.State.Messages);

                JsPublish(c, Subject(1), 10);
                si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(10u, si.State.Messages);

                PurgeOptions options = PurgeOptions.Builder().WithKeep(7).Build();
                PurgeResponse pr     = jsm.PurgeStream(STREAM, options);
                Assert.True(pr.Success);
                Assert.Equal(3u, pr.Purged);

                options = PurgeOptions.Builder().WithSequence(9).Build();
                pr      = jsm.PurgeStream(STREAM, options);
                Assert.True(pr.Success);
                Assert.Equal(5u, pr.Purged);
                si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(2u, si.State.Messages);

                pr = jsm.PurgeStream(STREAM);
                Assert.True(pr.Success);
                Assert.Equal(2u, pr.Purged);
                si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(0u, si.State.Messages);

                JsPublish(c, Subject(1), 10);
                JsPublish(c, Subject(2), 10);
                si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(20u, si.State.Messages);
                jsm.PurgeStream(STREAM, PurgeOptions.WithSubject(Subject(1)));
                si = jsm.GetStreamInfo(STREAM);
                Assert.Equal(10u, si.State.Messages);

                options = PurgeOptions.Builder().WithSubject(Subject(1)).WithSequence(1).Build();
                Assert.Equal(Subject(1), options.Subject);
                Assert.Equal(1u, options.Sequence);

                options = PurgeOptions.Builder().WithSubject(Subject(1)).WithKeep(2).Build();
                Assert.Equal(2u, options.Keep);
            });
        }
예제 #5
0
        public async Task <PurgeResponse> PurgeAsync(PurgeRequest request)
        {
            var maxRequestSize = _cdnApis.Min(c => c.MaxBatchSize);

            var batches = request.Urls.Batch(maxRequestSize, c => new PurgeRequest(c));

            var purgeTasks = _cdnApis.Where(c => c.IsEnabled())
                             .SelectMany(cdn => batches.Select(cdn.PurgeByUrlAsync));

            var results = await Task.WhenAll(purgeTasks);

            return(PurgeResponse.Aggregate(results));
        }
예제 #6
0
        public void Aggregate_GivenPartialSuccess_ThenNotSuccessful()
        {
            var subjects = new[]
            {
                new PurgeResponse(PurgeResult.Success, null, null, null),
                new PurgeResponse(PurgeResult.Fail, null, null, null)
            };

            var result = PurgeResponse.Aggregate(subjects);

            Assert.AreEqual(PurgeResult.Fail, result.Result);

            // Default assertions
            Assert.AreEqual(0, result.FailedUrls.Count());
            Assert.AreEqual(0, result.FailMessages.Count());
            Assert.IsNull(result.Exception);
        }
예제 #7
0
        public void Aggregate_GivenFailedUrls_ThenAggregate()
        {
            var subjects = new[]
            {
                new PurgeResponse(PurgeResult.Fail, CdnType.CloudFlare, new [] { "mock/1", "mock/2" }, null, null),
                new PurgeResponse(PurgeResult.Success, CdnType.CloudFlare, null, null, null),
                new PurgeResponse(PurgeResult.Fail, CdnType.CloudFlare, new [] { "mock/3" }, null, null)
            };

            var result = PurgeResponse.Aggregate(subjects);

            Assert.AreEqual(1, result.FailedUrls.Count());
            Assert.AreEqual(3, result.FailedUrls[CdnType.CloudFlare].Count());

            // Default assertions
            Assert.AreEqual(PurgeResult.Fail, result.Result);
            Assert.AreEqual(0, result.FailMessages.Count());
            Assert.IsNull(result.Exception);
        }
예제 #8
0
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("JetStream Manage Streams", args, Usage)
                                    .DefaultStream("manage-stream-")
                                    .DefaultSubject("manage-subject-")
                                    .Build();

            string stream1  = helper.Stream + "1";
            string stream2  = helper.Stream + "2";
            string subject1 = helper.Subject + "1";
            string subject2 = helper.Subject + "2";
            string subject3 = helper.Subject + "3";
            string subject4 = helper.Subject + "4";

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Create a JetStreamManagement context.
                    IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                    // we want to be able to completely create and delete the streams
                    // so don't want to work with existing streams
                    JsUtils.ExitIfStreamExists(jsm, stream1);
                    JsUtils.ExitIfStreamExists(jsm, stream2);

                    // 1. Create (add) a stream with a subject
                    Console.WriteLine("\n----------\n1. Configure And Add Stream 1");
                    StreamConfiguration streamConfig = StreamConfiguration.Builder()
                                                       .WithName(stream1)
                                                       .WithSubjects(subject1)
                                                       // .WithRetentionPolicy(...)
                                                       // .WithMaxConsumers(...)
                                                       // .WithMaxBytes(...)
                                                       // .WithMaxAge(...)
                                                       // .WithMaxMsgSize(...)
                                                       .WithStorageType(StorageType.Memory)
                                                       // .WithReplicas(...)
                                                       // .WithNoAck(...)
                                                       // .WithTemplateOwner(...)
                                                       // .WithDiscardPolicy(...)
                                                       .Build();

                    StreamInfo streamInfo = jsm.AddStream(streamConfig);
                    PrintUtils.PrintStreamInfo(streamInfo);

                    // 2. Update stream, in this case add a subject
                    //    Thre are very few properties that can actually
                    // -  StreamConfiguration is immutable once created
                    // -  but the builder can help with that.
                    Console.WriteLine("----------\n2. Update Stream 1");
                    streamConfig = StreamConfiguration.Builder(streamInfo.Config)
                                   .AddSubjects(subject2).Build();
                    streamInfo = jsm.UpdateStream(streamConfig);
                    PrintUtils.PrintStreamInfo(streamInfo);

                    // 3. Create (add) another stream with 2 subjects
                    Console.WriteLine("----------\n3. Configure And Add Stream 2");
                    streamConfig = StreamConfiguration.Builder()
                                   .WithName(stream2)
                                   .WithSubjects(subject3, subject4)
                                   .WithStorageType(StorageType.Memory)
                                   .Build();
                    streamInfo = jsm.AddStream(streamConfig);
                    PrintUtils.PrintStreamInfo(streamInfo);

                    // 4. Get information on streams
                    // 4.0 publish some message for more interesting stream state information
                    // -   SUBJECT1 is associated with STREAM1
                    // 4.1 getStreamInfo on a specific stream
                    // 4.2 get a list of all streams
                    // 4.3 get a list of StreamInfo's for all streams
                    Console.WriteLine("----------\n4.1 getStreamInfo");
                    JsUtils.Publish(c, subject1, 5);
                    streamInfo = jsm.GetStreamInfo(stream1);
                    PrintUtils.PrintStreamInfo(streamInfo);

                    Console.WriteLine("----------\n4.2 getStreamNames");
                    IList <String> streamNames = jsm.GetStreamNames();
                    PrintUtils.PrintObject(streamNames);

                    Console.WriteLine("----------\n4.3 getStreams");
                    IList <StreamInfo> streamInfos = jsm.GetStreams();
                    PrintUtils.PrintStreamInfoList(streamInfos);

                    // 5. Purge a stream of it's messages
                    Console.WriteLine("----------\n5. Purge stream");
                    PurgeResponse purgeResponse = jsm.PurgeStream(stream1);
                    PrintUtils.PrintObject(purgeResponse);

                    // 6. Delete the streams
                    // Subsequent calls to getStreamInfo, deleteStream or purgeStream
                    // will throw a JetStreamApiException "stream not found [10059]"
                    Console.WriteLine("----------\n6. Delete streams");
                    jsm.DeleteStream(stream1);
                    jsm.DeleteStream(stream2);

                    // 7. Try to delete the consumer again and get the exception
                    Console.WriteLine("----------\n7. Delete stream again");
                    try
                    {
                        jsm.DeleteStream(stream1);
                    }
                    catch (NATSJetStreamException e)
                    {
                        Console.WriteLine($"Exception was: '{e.ErrorDescription}'");
                    }

                    Console.WriteLine("----------\n");
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }
예제 #9
0
 public static PurgeResponseAssertions Should(this PurgeResponse response)
 {
     return(new PurgeResponseAssertions(response));
 }
예제 #10
0
 public PurgeResponseAssertions(PurgeResponse response)
 {
     Response = response;
 }