public void Only_allows_1_BgThread_to_run_at_a_time() { mqHost = CreateMqHost(); mqHost.RegisterHandler <Reverse>(x => x.GetBody().Value.Reverse()); mqHost.RegisterHandler <Rot13>(x => x.GetBody().Value.ToRot13()); 5.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.BgThreadCount, Is.EqualTo(1)); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Stop())); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Stopped")); ThreadPool.QueueUserWorkItem(y => mqHost.Start()); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.BgThreadCount, Is.EqualTo(2)); Debug.WriteLine(mqHost.GetStats()); mqHost.Dispose(); }
public void Does_process_all_messages_and_Starts_Stops_correctly_with_multiple_threads_racing() { mqHost = CreateMqHost(); var reverseCalled = 0; var rot13Called = 0; mqHost.RegisterHandler <Reverse>(x => { reverseCalled++; return(x.GetBody().Value.Reverse()); }); mqHost.RegisterHandler <Rot13>(x => { rot13Called++; return(x.GetBody().Value.ToRot13()); }); var mqClient = mqHost.CreateMessageQueueClient(); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); mqHost.Start(); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(3)); mqClient.Publish(new Reverse { Value = "Foo" }); mqClient.Publish(new Rot13 { Value = "Bar" }); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); 5.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Stop())); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Stopped").Or.EqualTo("Stopping")); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Debug.WriteLine("\n" + mqHost.GetStats()); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(5)); Assert.That(reverseCalled, Is.EqualTo(3)); Assert.That(rot13Called, Is.EqualTo(2)); mqHost.Dispose(); }
public void Can_receive_and_process_same_reply_responses() { mqHost = CreateMqHost(); var called = 0; mqHost.RegisterHandler <Incr>(m => { Debug.WriteLine("In Incr #" + m.GetBody().Value); called++; return(m.GetBody().Value > 0 ? new Incr { Value = m.GetBody().Value - 1 } : null); }); mqHost.Start(); var mqClient = mqHost.CreateMessageQueueClient(); var incr = new Incr { Value = 5 }; mqClient.Publish(incr); Thread.Sleep(2000); mqHost.Dispose(); Assert.That(called, Is.EqualTo(1 + incr.Value)); }
public void Can_receive_and_process_standard_request_reply_combo() { mqHost = CreateMqHost(); string messageReceived = null; mqHost.RegisterHandler <Hello>(m => new HelloResponse { Result = "Hello, " + m.GetBody().Name }); mqHost.RegisterHandler <HelloResponse>(m => { messageReceived = m.GetBody().Result; return(null); }); mqHost.Start(); var mqClient = mqHost.CreateMessageQueueClient(); var dto = new Hello { Name = "ServiceStack" }; mqClient.Publish(dto); Thread.Sleep(2000); mqHost.Dispose(); Assert.That(messageReceived, Is.EqualTo("Hello, ServiceStack")); }
public override void Configure(Container container) { var redisFactory = new PooledRedisClientManager("localhost:6379"); _mqHost = new RedisMqHost(redisFactory); _mqHost.Start(); }
public override void Configure(Funq.Container container) { var redisfactrory = new PooledRedisClientManager("localhost:6379"); _mqHost = new RedisMqHost(redisfactrory); _mqHost.RegisterHandler<EntryResponse>(message => { Console.WriteLine("Got message id {0}", message.GetBody().Id); return null; }); _mqHost.Start(); }
public override void Configure(Container container) { var redisFactory = new PooledRedisClientManager("localhost:6379"); mqHost = new RedisMqHost(redisFactory); mqHost.RegisterHandler <EntryResponse>(message => { Console.WriteLine("Got message id: {0}", message.GetBody().Id); return(null); }); mqHost.Start(); }
public void Cannot_Start_a_Disposed_MqHost() { mqHost = CreateMqHost(); mqHost.RegisterHandler <Reverse>(x => x.GetBody().Value.Reverse()); mqHost.Dispose(); try { mqHost.Start(); Assert.Fail("Should throw ObjectDisposedException"); } catch (ObjectDisposedException) { } }
public void Does_process_messages_sent_before_it_was_started() { var reverseCalled = 0; mqHost = CreateMqHost(); mqHost.RegisterHandler <Reverse>(x => { reverseCalled++; return(x.GetBody().Value.Reverse()); }); var mqClient = mqHost.CreateMessageQueueClient(); Publish_4_messages(mqClient); mqHost.Start(); Thread.Sleep(3000); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(4)); Assert.That(reverseCalled, Is.EqualTo(4)); mqHost.Dispose(); }
public override void Configure(Container container) { JsConfig.EmitCamelCaseNames = true; this.RequestFilters.Add((req, res, dto) => { var requestFilter = dto as RequestFilter; if (requestFilter != null) { res.StatusCode = requestFilter.StatusCode; if (!requestFilter.HeaderName.IsNullOrEmpty()) { res.AddHeader(requestFilter.HeaderName, requestFilter.HeaderValue); } res.Close(); } var secureRequests = dto as IRequiresSession; if (secureRequests != null) { res.ReturnAuthRequired(); } }); this.Container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory( "~/App_Data/db.sqlite".MapHostAbsolutePath(), SqliteOrmLiteDialectProvider.Instance) { ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current) }); this.Container.Register<ICacheClient>(new MemoryCacheClient()); //this.Container.Register<ICacheClient>(new BasicRedisClientManager()); ConfigureAuth(container); //this.Container.Register<ISessionFactory>( // c => new SessionFactory(c.Resolve<ICacheClient>())); var dbFactory = this.Container.Resolve<IDbConnectionFactory>(); dbFactory.Exec(dbCmd => dbCmd.CreateTable<Movie>(true)); ModelConfig<Movie>.Id(x => x.Title); Routes .Add<Movies>("/custom-movies", "GET") .Add<Movies>("/custom-movies/genres/{Genre}") .Add<Movie>("/custom-movies", "POST,PUT") .Add<Movie>("/custom-movies/{Id}") .Add<MqHostStats>("/mqstats"); var resetMovies = this.Container.Resolve<ResetMoviesService>(); resetMovies.Post(null); Plugins.Add(new ValidationFeature()); Plugins.Add(new SessionFeature()); Plugins.Add(new ProtoBufFormat()); container.RegisterValidators(typeof(CustomersValidator).Assembly); //var onlyEnableFeatures = Feature.All.Remove(Feature.Jsv | Feature.Soap); SetConfig(new EndpointHostConfig { //EnableFeatures = onlyEnableFeatures, DebugMode = true, //Show StackTraces for easier debugging }); var redisManager = new BasicRedisClientManager(); var mqHost = new RedisMqHost(redisManager, 2, null); mqHost.RegisterHandler<Reverse>(this.Container.Resolve<ReverseService>().Execute); mqHost.Start(); this.Container.Register((IMessageService)mqHost); }
public void Does_process_all_messages_and_Starts_Stops_correctly_with_multiple_threads_racing() { mqHost = CreateMqHost(); var reverseCalled = 0; var rot13Called = 0; mqHost.RegisterHandler<Reverse>(x => { reverseCalled++; return x.GetBody().Value.Reverse(); }); mqHost.RegisterHandler<Rot13>(x => { rot13Called++; return x.GetBody().Value.ToRot13(); }); var mqClient = mqHost.CreateMessageQueueClient(); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); mqHost.Start(); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(3)); mqClient.Publish(new Reverse { Value = "Foo" }); mqClient.Publish(new Rot13 { Value = "Bar" }); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); 5.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Stop())); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Stopped").Or.EqualTo("Stopping")); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Thread.Sleep(3000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Debug.WriteLine("\n" + mqHost.GetStats()); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(5)); Assert.That(reverseCalled, Is.EqualTo(3)); Assert.That(rot13Called, Is.EqualTo(2)); mqHost.Dispose(); }
public void Only_allows_1_BgThread_to_run_at_a_time() { mqHost = CreateMqHost(); mqHost.RegisterHandler<Reverse>(x => x.GetBody().Value.Reverse()); mqHost.RegisterHandler<Rot13>(x => x.GetBody().Value.ToRot13()); 5.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Start())); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.BgThreadCount, Is.EqualTo(1)); 10.Times(x => ThreadPool.QueueUserWorkItem(y => mqHost.Stop())); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Stopped")); ThreadPool.QueueUserWorkItem(y => mqHost.Start()); Thread.Sleep(1000); Assert.That(mqHost.GetStatus(), Is.EqualTo("Started")); Assert.That(mqHost.BgThreadCount, Is.EqualTo(2)); Debug.WriteLine(mqHost.GetStats()); mqHost.Dispose(); }
public void Does_retry_messages_with_errors_by_RetryCount() { var retryCount = 3; var totalRetries = 1 + retryCount; //in total, inc. first try mqHost = CreateMqHost(retryCount); var reverseCalled = 0; var rot13Called = 0; mqHost.RegisterHandler <Reverse>(x => { reverseCalled++; return(x.GetBody().Value.Reverse()); }); mqHost.RegisterHandler <Rot13>(x => { rot13Called++; return(x.GetBody().Value.ToRot13()); }); mqHost.RegisterHandler <AlwaysThrows>(x => { throw new Exception("Always Throwing! " + x.GetBody().Value); }); mqHost.Start(); Thread.Sleep(3000); var mqClient = mqHost.CreateMessageQueueClient(); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); mqClient.Publish(new AlwaysThrows { Value = "1st" }); Thread.Sleep(3000); Assert.That(mqHost.GetStats().TotalMessagesFailed, Is.EqualTo(1 * totalRetries)); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(2 + 1)); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); 5.Times(x => mqClient.Publish(new AlwaysThrows { Value = "#" + x })); Thread.Sleep(10000); Debug.WriteLine(mqHost.GetStatsDescription()); Assert.That(mqHost.GetStats().TotalMessagesFailed, Is.EqualTo((1 + 5) * totalRetries)); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(6)); Assert.That(reverseCalled, Is.EqualTo(2 + 2)); Assert.That(rot13Called, Is.EqualTo(1 + 1)); mqHost.Dispose(); }
public void Cannot_Stop_a_Disposed_MqHost() { mqHost = CreateMqHost(); mqHost.RegisterHandler<Reverse>(x => x.GetBody().Value.Reverse()); mqHost.Start(); Thread.Sleep(5000); mqHost.Dispose(); try { mqHost.Stop(); Assert.Fail("Should throw ObjectDisposedException"); } catch (ObjectDisposedException) { } }
public void Does_retry_messages_with_errors_by_RetryCount() { var retryCount = 3; var totalRetries = 1 + retryCount; //in total, inc. first try mqHost = CreateMqHost(retryCount); var reverseCalled = 0; var rot13Called = 0; mqHost.RegisterHandler<Reverse>(x => { reverseCalled++; return x.GetBody().Value.Reverse(); }); mqHost.RegisterHandler<Rot13>(x => { rot13Called++; return x.GetBody().Value.ToRot13(); }); mqHost.RegisterHandler<AlwaysThrows>(x => { throw new Exception("Always Throwing! " + x.GetBody().Value); }); mqHost.Start(); Thread.Sleep(3000); var mqClient = mqHost.CreateMessageQueueClient(); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); mqClient.Publish(new AlwaysThrows { Value = "1st" }); Thread.Sleep(3000); Assert.That(mqHost.GetStats().TotalMessagesFailed, Is.EqualTo(1 * totalRetries)); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(2 + 1)); mqClient.Publish(new Reverse { Value = "Hello" }); mqClient.Publish(new Reverse { Value = "World" }); mqClient.Publish(new Rot13 { Value = "ServiceStack" }); 5.Times(x => mqClient.Publish(new AlwaysThrows { Value = "#" + x })); Thread.Sleep(10000); Debug.WriteLine(mqHost.GetStatsDescription()); Assert.That(mqHost.GetStats().TotalMessagesFailed, Is.EqualTo((1 + 5) * totalRetries)); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(6)); Assert.That(reverseCalled, Is.EqualTo(2 + 2)); Assert.That(rot13Called, Is.EqualTo(1 + 1)); mqHost.Dispose(); }
public override void Configure(Container container) { JsConfig.EmitCamelCaseNames = true; this.RequestFilters.Add((req, res, dto) => { var requestFilter = dto as RequestFilter; if (requestFilter != null) { res.StatusCode = requestFilter.StatusCode; if (!requestFilter.HeaderName.IsNullOrEmpty()) { res.AddHeader(requestFilter.HeaderName, requestFilter.HeaderValue); } res.Close(); } var secureRequests = dto as IRequiresSession; if (secureRequests != null) { res.ReturnAuthRequired(); } }); this.Container.Register <IDbConnectionFactory>(c => new OrmLiteConnectionFactory( "~/App_Data/db.sqlite".MapHostAbsolutePath(), SqliteOrmLiteDialectProvider.Instance) { ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current) }); this.Container.Register <ICacheClient>(new MemoryCacheClient()); //this.Container.Register<ICacheClient>(new BasicRedisClientManager()); ConfigureAuth(container); //this.Container.Register<ISessionFactory>( // c => new SessionFactory(c.Resolve<ICacheClient>())); var dbFactory = this.Container.Resolve <IDbConnectionFactory>(); dbFactory.Run(db => db.CreateTable <Movie>(true)); ModelConfig <Movie> .Id(x => x.Title); Routes .Add <Movies>("/custom-movies", "GET, OPTIONS") .Add <Movies>("/custom-movies/genres/{Genre}") .Add <Movie>("/custom-movies", "POST,PUT") .Add <Movie>("/custom-movies/{Id}") .Add <MqHostStats>("/mqstats"); var resetMovies = this.Container.Resolve <ResetMoviesService>(); resetMovies.Post(null); Plugins.Add(new ValidationFeature()); Plugins.Add(new SessionFeature()); Plugins.Add(new ProtoBufFormat()); container.RegisterValidators(typeof(CustomersValidator).Assembly); container.Register(c => new FunqSingletonScope()).ReusedWithin(ReuseScope.Default); container.Register(c => new FunqRequestScope()).ReusedWithin(ReuseScope.Request); container.Register(c => new FunqNoneScope()).ReusedWithin(ReuseScope.None); Routes.Add <IocScope>("/iocscope"); //var onlyEnableFeatures = Feature.All.Remove(Feature.Jsv | Feature.Soap); SetConfig(new EndpointHostConfig { GlobalResponseHeaders = { { "Access-Control-Allow-Origin", "*" }, { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" }, { "Access-Control-Allow-Headers", "Content-Type, X-Requested-With" }, }, //EnableFeatures = onlyEnableFeatures, DebugMode = true, //Show StackTraces for easier debugging }); var redisManager = new BasicRedisClientManager(); var mqHost = new RedisMqHost(redisManager, 2, null); mqHost.RegisterHandler <Reverse>(this.Container.Resolve <ReverseService>().Execute); mqHost.Start(); this.Container.Register((IMessageService)mqHost); }
public override void Configure(Container container) { this.RequestFilters.Add((req, res, dto) => { var requestFilter = dto as RequestFilter; if (requestFilter != null) { res.StatusCode = requestFilter.StatusCode; if (!requestFilter.HeaderName.IsNullOrEmpty()) { res.AddHeader(requestFilter.HeaderName, requestFilter.HeaderValue); } res.Close(); } var secureRequests = dto as IRequiresSession; if (secureRequests != null) { res.ReturnAuthRequired(); } }); this.Container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory( "~/App_Data/db.sqlite".MapHostAbsolutePath(), SqliteOrmLiteDialectProvider.Instance)); this.Container.Register<ICacheClient>(new MemoryCacheClient()); var dbFactory = this.Container.Resolve<IDbConnectionFactory>(); dbFactory.Exec(dbCmd => dbCmd.CreateTable<Movie>(true)); Routes .Add<Movies>("/custom-movies", "GET") .Add<Movies>("/custom-movies/genres/{Genre}") .Add<Movie>("/custom-movies", "POST,PUT") .Add<Movie>("/custom-movies/{Id}") .Add<MqHostStats>("/mqstats"); var resetMovies = this.Container.Resolve<ResetMoviesService>(); resetMovies.Post(null); //var onlyEnableFeatures = Feature.All.Remove(Feature.Jsv | Feature.Soap); //SetConfig(new EndpointHostConfig //{ // EnableFeatures = onlyEnableFeatures, // DebugMode = true, //Show StackTraces for easier debugging //}); var redisManager = new BasicRedisClientManager(); var mqHost = new RedisMqHost(redisManager, 2, null); mqHost.RegisterHandler<Reverse>(x => this.Container.Resolve<ReverseService>().Execute(x.GetBody())); mqHost.Start(); this.Container.Register((IMessageService)mqHost); }
public override void Configure(Container container) { JsConfig.EmitCamelCaseNames = true; this.RequestFilters.Add((req, res, dto) => { var requestFilter = dto as RequestFilter; if (requestFilter != null) { res.StatusCode = requestFilter.StatusCode; if (!requestFilter.HeaderName.IsNullOrEmpty()) { res.AddHeader(requestFilter.HeaderName, requestFilter.HeaderValue); } res.Close(); } var secureRequests = dto as IRequiresSession; if (secureRequests != null) { res.ReturnAuthRequired(); } }); this.Container.Register <IDbConnectionFactory>(c => new OrmLiteConnectionFactory( "~/App_Data/db.sqlite".MapHostAbsolutePath(), SqliteOrmLiteDialectProvider.Instance) { ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current) }); this.Container.Register <ICacheClient>(new MemoryCacheClient()); //this.Container.Register<ICacheClient>(new BasicRedisClientManager()); ConfigureAuth(container); //this.Container.Register<ISessionFactory>( // c => new SessionFactory(c.Resolve<ICacheClient>())); var dbFactory = this.Container.Resolve <IDbConnectionFactory>(); dbFactory.Exec(dbCmd => dbCmd.CreateTable <Movie>(true)); ModelConfig <Movie> .Id(x => x.Title); Routes .Add <Movies>("/custom-movies", "GET") .Add <Movies>("/custom-movies/genres/{Genre}") .Add <Movie>("/custom-movies", "POST,PUT") .Add <Movie>("/custom-movies/{Id}") .Add <MqHostStats>("/mqstats"); var resetMovies = this.Container.Resolve <ResetMoviesService>(); resetMovies.Post(null); ValidationFeature.Init(this); container.RegisterValidators(typeof(CustomersValidator).Assembly); SessionFeature.Init(this); //var onlyEnableFeatures = Feature.All.Remove(Feature.Jsv | Feature.Soap); SetConfig(new EndpointHostConfig { //EnableFeatures = onlyEnableFeatures, DebugMode = true, //Show StackTraces for easier debugging }); var redisManager = new BasicRedisClientManager(); var mqHost = new RedisMqHost(redisManager, 2, null); mqHost.RegisterHandler <Reverse>(this.Container.Resolve <ReverseService>().Execute); mqHost.Start(); this.Container.Register((IMessageService)mqHost); }
public void Can_receive_and_process_same_reply_responses() { mqHost = CreateMqHost(); var called = 0; mqHost.RegisterHandler<Incr>(m => { Debug.WriteLine("In Incr #" + m.GetBody().Value); called++; return m.GetBody().Value > 0 ? new Incr { Value = m.GetBody().Value - 1 } : null; }); mqHost.Start(); var mqClient = mqHost.CreateMessageQueueClient(); var incr = new Incr { Value = 5 }; mqClient.Publish(incr); Thread.Sleep(2000); mqHost.Dispose(); Assert.That(called, Is.EqualTo(1 + incr.Value)); }
public void Does_process_messages_sent_before_it_was_started() { var reverseCalled = 0; mqHost = CreateMqHost(); mqHost.RegisterHandler<Reverse>(x => { reverseCalled++; return x.GetBody().Value.Reverse(); }); var mqClient = mqHost.CreateMessageQueueClient(); Publish_4_messages(mqClient); mqHost.Start(); Thread.Sleep(3000); Assert.That(mqHost.GetStats().TotalMessagesProcessed, Is.EqualTo(4)); Assert.That(reverseCalled, Is.EqualTo(4)); mqHost.Dispose(); }
public void Can_receive_and_process_standard_request_reply_combo() { mqHost = CreateMqHost(); string messageReceived = null; mqHost.RegisterHandler<Hello>(m => new HelloResponse { Result = "Hello, " + m.GetBody().Name }); mqHost.RegisterHandler<HelloResponse>(m => { messageReceived = m.GetBody().Result; return null; }); mqHost.Start(); var mqClient = mqHost.CreateMessageQueueClient(); var dto = new Hello { Name = "ServiceStack" }; mqClient.Publish(dto); Thread.Sleep(2000); mqHost.Dispose(); Assert.That(messageReceived, Is.EqualTo("Hello, ServiceStack")); }