public void TestExponentialBackoff() { var expected = new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(8), TimeSpan.FromSeconds(32) }; var backoffStrategy = new ExponentialStrategy(); var config = new NsqConfig(); config.BackoffMultiplier = TimeSpan.FromSeconds(1); var attempts = new[] { 1, 2, 4, 6 }; for (var i = 0; i < attempts.Length; i++) { var result = backoffStrategy.Calculate(config, attempts[i]); Assert.AreEqual(expected[i], result.Duration, string.Format("wrong backoff duration for attempt {0}", attempts[i])); } }
public void TestProducerMultiPublish() { var topicName = "multi_publish" + DateTime.Now.Unix(); const int msgCount = 10; var config = new NsqConfig(); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); try { var testData = new List <byte[]>(); for (int i = 0; i < msgCount; i++) { testData.Add(Encoding.UTF8.GetBytes("multipublish_test_case")); } w.MultiPublish(topicName, testData); w.Publish(topicName, "bad_test_case"); readMessages(topicName, msgCount); } finally { w.Stop(); _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
public void TestFullJitterBackoff() { // afaik there's no way to seed a RNGCryptoServiceProvider (probably a good thing) // we'll test the intent: the range will be between 0 and 2^n*backoffmultiplier var maxExpected = new[] { TimeSpan.FromSeconds(0.5), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(16) }; var backoffStrategy = new FullJitterStrategy(); var config = new NsqConfig(); config.BackoffMultiplier = TimeSpan.FromSeconds(0.5); var attempts = new[] { 1, 2, 4, 6 }; for (var count = 0; count < 50000; count++) { for (var i = 0; i < attempts.Length; i++) { var attempt = attempts[i]; var result = backoffStrategy.Calculate(config, attempt).Duration; AssertHelper.LessOrEqual(result, maxExpected[i], string.Format("wrong backoff duration for attempt {0}", attempt)); } } }
/// <summary> /// Initializes a new instance of the <see cref="Conn"/> class. /// </summary> public Conn(string addr, NsqConfig config, IConnDelegate connDelegate) { if (string.IsNullOrEmpty(addr)) { throw new ArgumentNullException("addr"); } if (config == null) { throw new ArgumentNullException("config"); } if (connDelegate == null) { throw new ArgumentNullException("connDelegate"); } _addr = addr; _config = config.Clone(); _delegate = connDelegate; _maxRdyCount = 2500; _lastMsgTimestamp = DateTime.Now.UnixNano(); _cmdChan = new Chan <Command>(); _msgResponseChan = new Chan <MessageResponse>(); _exitChan = new Chan <int>(); _drainReady = new Chan <int>(); }
public void TestConfigValidateError() { var c = new NsqConfig(); AssertHelper.Throws <Exception>(() => c.Set("max_backoff_duration", "24h")); // property wasn't set, state should still be ok c.Validate(); // test MaxBackoffDuration validation c.MaxBackoffDuration = TimeSpan.FromHours(24); Assert.AreEqual(TimeSpan.FromHours(24), c.MaxBackoffDuration); AssertHelper.Throws <Exception>(c.Validate); // reset to good state c.MaxBackoffDuration = TimeSpan.FromSeconds(60); c.Validate(); // make sure another bad value and call to Validate still throws after the good Validate c.MaxBackoffDuration = TimeSpan.FromHours(24); Assert.AreEqual(TimeSpan.FromHours(24), c.MaxBackoffDuration); AssertHelper.Throws <Exception>(c.Validate); // reset to good state c.MaxBackoffDuration = TimeSpan.FromSeconds(60); c.Validate(); // test null BackoffStrategy validation c.BackoffStrategy = null; Assert.IsNull(c.BackoffStrategy); AssertHelper.Throws <Exception>(c.Validate); }
public void TestConfigSet() { var c = new NsqConfig(); AssertHelper.Throws <Exception>(() => c.Set("not a real config value", new object()), "No error when setting an invalid value"); }
public void TestDefaultValues() { var c = new NsqConfig(); Assert.AreEqual(TimeSpan.FromSeconds(1), c.DialTimeout, "dial_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(60), c.ReadTimeout, "read_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(10), c.WriteTimeout, "write_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(60), c.LookupdPollInterval, "lookupd_poll_interval"); Assert.AreEqual(0.3, c.LookupdPollJitter, "lookupd_poll_jitter"); Assert.AreEqual(TimeSpan.FromMinutes(15), c.MaxRequeueDelay, "max_requeue_delay"); Assert.AreEqual(TimeSpan.FromSeconds(90), c.DefaultRequeueDelay, "default_requeue_delay"); Assert.AreEqual(typeof(ExponentialStrategy), c.BackoffStrategy.GetType(), "backoff_strategy"); Assert.AreEqual(TimeSpan.FromMinutes(2), c.MaxBackoffDuration, "max_backoff_duration"); Assert.AreEqual(TimeSpan.FromSeconds(1), c.BackoffMultiplier, "backoff_multiplier"); Assert.AreEqual(5, c.MaxAttempts, "max_attempts"); Assert.AreEqual(TimeSpan.FromSeconds(10), c.LowRdyIdleTimeout, "low_rdy_idle_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(5), c.RDYRedistributeInterval, "rdy_redistribute_interval"); Assert.AreEqual(false, c.RDYRedistributeOnIdle, "rdy_redistribute_on_idle"); Assert.AreEqual(OS.Hostname().Split('.')[0], c.ClientID, "client_id"); Assert.AreEqual(OS.Hostname(), c.Hostname, "hostname"); Assert.AreEqual(string.Format("{0}/{1}", ClientInfo.ClientName, ClientInfo.Version), c.UserAgent, "user_agent"); Assert.AreEqual(TimeSpan.FromSeconds(30), c.HeartbeatInterval, "heartbeat_interval"); Assert.AreEqual(0, c.SampleRate, "sample_rate"); Assert.IsNull(c.TlsConfig, "tls_config"); //Assert.AreEqual(false, c.Deflate, "deflate"); // TODO: Deflate //Assert.AreEqual(6, c.DeflateLevel, "deflate_level"); // TODO: Deflate //Assert.AreEqual(false, c.Snappy, "snappy"); // TODO: Snappy Assert.AreEqual(16384, c.OutputBufferSize, "output_buffer_size"); Assert.AreEqual(TimeSpan.FromMilliseconds(250), c.OutputBufferTimeout, "output_buffer_timeout"); Assert.AreEqual(1, c.MaxInFlight, "max_in_flight"); Assert.AreEqual(TimeSpan.Zero, c.MessageTimeout, "msg_timeout"); Assert.IsNull(c.AuthSecret, "auth_secret"); }
public void TestProducerPublish() { var topicName = "publish" + DateTime.Now.Unix(); const int msgCount = 10; var config = new NsqConfig(); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); try { for (int i = 0; i < msgCount; i++) { w.Publish(topicName, "publish_test_case"); } w.Publish(topicName, "bad_test_case"); readMessages(topicName, msgCount); } finally { w.Stop(); _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
public void TestBackoffStrategyCoerce() { var c = new NsqConfig(); c.Set("backoff_strategy", "exponential"); Assert.AreEqual(typeof(ExponentialStrategy), c.BackoffStrategy.GetType()); c.Set("backoff_strategy", ""); Assert.AreEqual(typeof(ExponentialStrategy), c.BackoffStrategy.GetType()); c.Set("backoff_strategy", null); Assert.IsNull(c.BackoffStrategy); c.Set("backoff_strategy", "full_jitter"); Assert.AreEqual(typeof(FullJitterStrategy), c.BackoffStrategy.GetType()); AssertHelper.Throws <Exception>(() => c.Set("backoff_strategy", "invalid")); var fullJitterStrategy = new FullJitterStrategy(); c.Set("backoff_strategy", fullJitterStrategy); Assert.AreEqual(fullJitterStrategy, c.BackoffStrategy); var exponentialStrategy = new ExponentialStrategy(); c.Set("backoff_strategy", exponentialStrategy); Assert.AreEqual(exponentialStrategy, c.BackoffStrategy); AssertHelper.Throws <Exception>(() => c.Set("backoff_strategy", new object())); }
public void TestMaxValues() { var c = new NsqConfig(); var tlsConfig = new TlsConfig(); c.Set("read_timeout", TimeSpan.FromMinutes(5)); c.Set("write_timeout", TimeSpan.FromMinutes(5)); c.Set("lookupd_poll_interval", TimeSpan.FromMinutes(5)); c.Set("lookupd_poll_jitter", 1); c.Set("max_requeue_delay", TimeSpan.FromMinutes(60)); c.Set("default_requeue_delay", TimeSpan.FromMinutes(60)); c.Set("backoff_strategy", "full_jitter"); c.Set("max_backoff_duration", TimeSpan.FromMinutes(60)); c.Set("backoff_multiplier", TimeSpan.FromMinutes(60)); c.Set("max_attempts", 65535); c.Set("low_rdy_idle_timeout", TimeSpan.FromMinutes(5)); c.Set("rdy_redistribute_interval", TimeSpan.FromSeconds(5)); c.Set("client_id", "my"); c.Set("hostname", "my.host.name.com"); c.Set("user_agent", "user-agent/1.0"); c.Set("heartbeat_interval", TimeSpan.MaxValue); c.Set("sample_rate", 99); c.Set("tls_config", tlsConfig); //c.Set("deflate", true); // TODO: Deflate //c.Set("deflate_level", 9); // TODO: Deflate //c.Set("snappy", true); // TODO: Snappy c.Set("output_buffer_size", long.MaxValue); c.Set("output_buffer_timeout", TimeSpan.MaxValue); c.Set("max_in_flight", int.MaxValue); c.Set("msg_timeout", TimeSpan.MaxValue); c.Set("auth_secret", "!@#@#$#%"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.ReadTimeout, "read_timeout"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.WriteTimeout, "write_timeout"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.LookupdPollInterval, "lookupd_poll_interval"); Assert.AreEqual(1, c.LookupdPollJitter, "lookupd_poll_jitter"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.MaxRequeueDelay, "max_requeue_delay"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.DefaultRequeueDelay, "default_requeue_delay"); Assert.AreEqual(typeof(FullJitterStrategy), c.BackoffStrategy.GetType(), "backoff_strategy"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.MaxBackoffDuration, "max_backoff_duration"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.BackoffMultiplier, "backoff_multiplier"); Assert.AreEqual(65535, c.MaxAttempts, "max_attempts"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.LowRdyIdleTimeout, "low_rdy_idle_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(5), c.RDYRedistributeInterval, "rdy_redistribute_interval"); Assert.AreEqual("my", c.ClientID, "client_id"); Assert.AreEqual("my.host.name.com", c.Hostname, "hostname"); Assert.AreEqual("user-agent/1.0", c.UserAgent, "user_agent"); Assert.AreEqual(TimeSpan.MaxValue, c.HeartbeatInterval, "heartbeat_interval"); Assert.AreEqual(99, c.SampleRate, "sample_rate"); Assert.AreEqual(tlsConfig, c.TlsConfig, "tls_config"); //Assert.AreEqual(true, c.Deflate, "deflate"); // TODO: Deflate //Assert.AreEqual(9, c.DeflateLevel, "deflate_level"); // TODO: Deflate //Assert.AreEqual(true, c.Snappy, "snappy"); // TODO: Snappy Assert.AreEqual(long.MaxValue, c.OutputBufferSize, "output_buffer_size"); Assert.AreEqual(TimeSpan.MaxValue, c.OutputBufferTimeout, "output_buffer_timeout"); Assert.AreEqual(int.MaxValue, c.MaxInFlight, "max_in_flight"); Assert.AreEqual(TimeSpan.MaxValue, c.MessageTimeout, "msg_timeout"); Assert.AreEqual("!@#@#$#%", c.AuthSecret, "auth_secret"); }
public void TestMinValues() { var c = new NsqConfig(); c.Set("read_timeout", TimeSpan.FromMilliseconds(100)); c.Set("write_timeout", TimeSpan.FromMilliseconds(100)); c.Set("lookupd_poll_interval", TimeSpan.FromMilliseconds(10)); c.Set("lookupd_poll_jitter", 0); c.Set("max_requeue_delay", TimeSpan.Zero); c.Set("default_requeue_delay", TimeSpan.Zero); c.Set("backoff_strategy", "exponential"); c.Set("max_backoff_duration", TimeSpan.Zero); c.Set("backoff_multiplier", "1ms"); c.Set("max_attempts", 0); c.Set("low_rdy_idle_timeout", TimeSpan.FromSeconds(1)); c.Set("rdy_redistribute_interval", TimeSpan.FromMilliseconds(1)); c.Set("client_id", null); c.Set("hostname", null); c.Set("user_agent", null); c.Set("heartbeat_interval", TimeSpan.MinValue); c.Set("sample_rate", 0); c.Set("tls_config", null); //c.Set("deflate", false); // TODO: Deflate //c.Set("deflate_level", 1); // TODO: Deflate //c.Set("snappy", false); // TODO: Snappy c.Set("output_buffer_size", long.MinValue); c.Set("output_buffer_timeout", TimeSpan.MinValue); c.Set("max_in_flight", 0); c.Set("msg_timeout", TimeSpan.Zero); c.Set("auth_secret", null); Assert.AreEqual(TimeSpan.FromMilliseconds(100), c.ReadTimeout, "read_timeout"); Assert.AreEqual(TimeSpan.FromMilliseconds(100), c.WriteTimeout, "write_timeout"); Assert.AreEqual(TimeSpan.FromMilliseconds(10), c.LookupdPollInterval, "lookupd_poll_interval"); Assert.AreEqual(0, c.LookupdPollJitter, "lookupd_poll_jitter"); Assert.AreEqual(TimeSpan.Zero, c.MaxRequeueDelay, "max_requeue_delay"); Assert.AreEqual(TimeSpan.Zero, c.DefaultRequeueDelay, "default_requeue_delay"); Assert.AreEqual(typeof(ExponentialStrategy), c.BackoffStrategy.GetType(), "backoff_strategy"); Assert.AreEqual(TimeSpan.Zero, c.MaxBackoffDuration, "max_backoff_duration"); Assert.AreEqual(TimeSpan.FromMilliseconds(1), c.BackoffMultiplier, "backoff_multiplier"); Assert.AreEqual(0, c.MaxAttempts, "max_attempts"); Assert.AreEqual(TimeSpan.FromSeconds(1), c.LowRdyIdleTimeout, "low_rdy_idle_timeout"); Assert.AreEqual(TimeSpan.FromMilliseconds(1), c.RDYRedistributeInterval, "rdy_redistribute_interval"); Assert.AreEqual(null, c.ClientID, "client_id"); Assert.AreEqual(null, c.Hostname, "hostname"); Assert.AreEqual(null, c.UserAgent, "user_agent"); Assert.AreEqual(TimeSpan.MinValue, c.HeartbeatInterval, "heartbeat_interval"); Assert.AreEqual(0, c.SampleRate, "sample_rate"); Assert.AreEqual(null, c.TlsConfig, "tls_config"); //Assert.AreEqual(false, c.Deflate, "deflate"); // TODO: Deflate //Assert.AreEqual(1, c.DeflateLevel, "deflate_level"); // TODO: Deflate //Assert.AreEqual(false, c.Snappy, "snappy"); // TODO: Snappy Assert.AreEqual(long.MinValue, c.OutputBufferSize, "output_buffer_size"); Assert.AreEqual(TimeSpan.MinValue, c.OutputBufferTimeout, "output_buffer_timeout"); Assert.AreEqual(0, c.MaxInFlight, "max_in_flight"); Assert.AreEqual(TimeSpan.Zero, c.MessageTimeout, "msg_timeout"); Assert.AreEqual(null, c.AuthSecret, "auth_secret"); }
public void TestHeartbeatLessThanReadTimeout() { var c = new NsqConfig(); c.Set("read_timeout", "5m"); c.Set("heartbeat_interval", "2s"); c.Validate(); c.Set("read_timeout", "2s"); c.Set("heartbeat_interval", "5m"); AssertHelper.Throws <Exception>(c.Validate); }
public void TestProducerHeartbeat() { var topicName = "heartbeat" + DateTime.Now.Unix(); var config = new NsqConfig(); config.HeartbeatInterval = TimeSpan.FromMilliseconds(100); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); try { ErrIdentify errIdentify = AssertHelper.Throws <ErrIdentify>(() => w.Publish(topicName, "publish_test_case")); Assert.AreEqual("E_BAD_BODY IDENTIFY heartbeat interval (100) is invalid", errIdentify.Reason); } finally { w.Stop(); // note: if test successful, topic will not be created - don't need to delete } try { config = new NsqConfig(); config.HeartbeatInterval = TimeSpan.FromMilliseconds(1000); w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); w.Publish(topicName, "publish_test_case"); // TODO: what are we testing here? Thread.Sleep(1100); const int msgCount = 10; for (int i = 0; i < msgCount; i++) { w.Publish(topicName, "publish_test_case"); } w.Publish(topicName, "bad_test_case"); readMessages(topicName, msgCount + 1); } finally { w.Stop(); _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
private void TestBackoffMaxLevel(IBackoffStrategy backoffStrategy) { for (var maxBackoff = 0; maxBackoff <= 16; maxBackoff++) { for (var multiplier = 1; multiplier <= 16; multiplier++) { var config = new NsqConfig { MaxBackoffDuration = TimeSpan.FromMilliseconds(maxBackoff), BackoffMultiplier = TimeSpan.FromMilliseconds(multiplier) }; config.Validate(); var expectedMaxLevel = 1; if (maxBackoff != 0 && multiplier != 0) { var x = config.MaxBackoffDuration.TotalSeconds / config.BackoffMultiplier.TotalSeconds; expectedMaxLevel = (int)Math.Ceiling(Math.Log(x, 2)) + 1; if (expectedMaxLevel <= 0) { expectedMaxLevel = 1; } } bool increaseBackoffLevel; if (expectedMaxLevel > 1) { increaseBackoffLevel = backoffStrategy.Calculate(config, expectedMaxLevel - 1).IncreaseBackoffLevel; Assert.IsTrue(increaseBackoffLevel, string.Format("increaseBackoff max={0} multiplier={1} level={2} expectedMaxLevel={3}", config.MaxBackoffDuration, config.BackoffMultiplier, expectedMaxLevel - 1, expectedMaxLevel)); } increaseBackoffLevel = backoffStrategy.Calculate(config, expectedMaxLevel).IncreaseBackoffLevel; Assert.IsFalse(increaseBackoffLevel, string.Format("increaseBackoff max={0} multiplier={1} level={2} expectedMaxLevel={3}", config.MaxBackoffDuration, config.BackoffMultiplier, expectedMaxLevel, expectedMaxLevel)); } } }
private void readMessages(string topicName, int msgCount) { var config = new NsqConfig(); config.DefaultRequeueDelay = TimeSpan.Zero; config.MaxBackoffDuration = TimeSpan.FromMilliseconds(50); var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config); var h = new ConsumerHandler { q = q }; q.AddHandler(h); q.ConnectToNsqd("127.0.0.1:4150"); q.Wait(); Assert.AreEqual(msgCount, h.messagesGood, "should have handled a diff number of messages"); Assert.AreEqual(1, h.messagesFailed, "failed message not done"); }
public void TestProducerConnection() { string topicName = "write_test" + DateTime.Now.Unix(); try { var config = new NsqConfig(); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); w.Publish(topicName, "test"); w.Stop(); AssertHelper.Throws <ErrStopped>(() => w.Publish(topicName, "fail test")); } finally { _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
public void TestProducerPublishAsync() { var topicName = "async_publish" + DateTime.Now.Unix(); const int msgCount = 10; var config = new NsqConfig(); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); try { var tasks = new List <Task <ProducerResponse> >(); for (int i = 0; i < msgCount; i++) { var task = w.PublishAsync(topicName, "publish_test_case", "test", i); tasks.Add(task); } for (int i = 0; i < msgCount; i++) { tasks[i].Wait(); var trans = tasks[i].Result; Assert.IsNull(trans.Error); Assert.IsNotNull(trans.Args); Assert.AreEqual(2, trans.Args.Length); Assert.AreEqual("test", trans.Args[0]); Assert.AreEqual(i, trans.Args[1]); } w.Publish(topicName, "bad_test_case"); readMessages(topicName, msgCount); } finally { w.Stop(); _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
public void TestValidatesGreaterThanMaxValues() { var c = new NsqConfig(); var tick = new TimeSpan(1); AssertHelper.Throws <Exception>(() => c.Set("read_timeout", TimeSpan.FromMinutes(5) + tick), "read_timeout"); AssertHelper.Throws <Exception>(() => c.Set("write_timeout", TimeSpan.FromMinutes(5) + tick), "write_timeout"); AssertHelper.Throws <Exception>(() => c.Set("lookupd_poll_interval", TimeSpan.FromMinutes(5) + tick), "lookupd_poll_interval"); AssertHelper.Throws <Exception>(() => c.Set("lookupd_poll_jitter", 1 + 0.0001), "lookupd_poll_jitter"); AssertHelper.Throws <Exception>(() => c.Set("max_requeue_delay", TimeSpan.FromMinutes(60) + tick), "max_requeue_delay"); AssertHelper.Throws <Exception>(() => c.Set("default_requeue_delay", TimeSpan.FromMinutes(60) + tick), "default_requeue_delay"); AssertHelper.Throws <Exception>(() => c.Set("backoff_strategy", "invalid"), "backoff_strategy"); AssertHelper.Throws <Exception>(() => c.Set("max_backoff_duration", TimeSpan.FromMinutes(60) + tick), "max_backoff_duration"); AssertHelper.Throws <Exception>(() => c.Set("backoff_multiplier", TimeSpan.FromMinutes(60) + tick), "backoff_multiplier"); AssertHelper.Throws <Exception>(() => c.Set("max_attempts", 65535 + 1), "max_attempts"); AssertHelper.Throws <Exception>(() => c.Set("low_rdy_idle_timeout", TimeSpan.FromMinutes(5) + tick), "low_rdy_idle_timeout"); AssertHelper.Throws <Exception>(() => c.Set("rdy_redistribute_interval", TimeSpan.FromMinutes(5) + tick), "rdy_redistribute_interval"); //AssertHelper.Throws<Exception>(() => c.Set("client_id", "my"), "client_id"); //AssertHelper.Throws<Exception>(() => c.Set("hostname", "my.host.name.com"), "hostname"); //AssertHelper.Throws<Exception>(() => c.Set("user_agent", "user-agent/1.0"), "user_agent"); //AssertHelper.Throws<Exception>(() => c.Set("heartbeat_interval", TimeSpan.MaxValue), "heartbeat_interval"); AssertHelper.Throws <Exception>(() => c.Set("sample_rate", 99 + 1), "sample_rate"); //AssertHelper.Throws<Exception>(() => c.Set("tls_config", tlsConfig); //AssertHelper.Throws<Exception>(() => c.Set("deflate", true), "deflate"); AssertHelper.Throws <Exception>(() => c.Set("deflate_level", 9 + 1), "deflate_level"); //AssertHelper.Throws<Exception>(() => c.Set("snappy", true), "snappy"); //AssertHelper.Throws<Exception>(() => c.Set("output_buffer_size", Int64.MaxValue), "output_buffer_size"); //AssertHelper.Throws<Exception>(() => c.Set("output_buffer_timeout", TimeSpan.MaxValue), "output_buffer_timeout"); //AssertHelper.Throws<Exception>(() => c.Set("max_in_flight", int.MaxValue), "max_in_flight"); //AssertHelper.Throws<Exception>(() => c.Set("msg_timeout", TimeSpan.MaxValue), "msg_timeout"); //AssertHelper.Throws<Exception>(() => c.Set("auth_secret", "!@#@#$#%"), "auth_secret"); }
public void TestValidatesLessThanMinValues() { var c = new NsqConfig(); var tick = new TimeSpan(1); AssertHelper.Throws <Exception>(() => c.Set("read_timeout", TimeSpan.FromMilliseconds(100) - tick), "read_timeout"); AssertHelper.Throws <Exception>(() => c.Set("write_timeout", TimeSpan.FromMilliseconds(100) - tick), "write_timeout"); AssertHelper.Throws <Exception>(() => c.Set("lookupd_poll_interval", TimeSpan.FromMilliseconds(10) - tick), "lookupd_poll_interval"); AssertHelper.Throws <Exception>(() => c.Set("lookupd_poll_jitter", 0 - double.Epsilon), "lookupd_poll_jitter"); AssertHelper.Throws <Exception>(() => c.Set("max_requeue_delay", TimeSpan.Zero - tick), "max_requeue_delay"); AssertHelper.Throws <Exception>(() => c.Set("default_requeue_delay", TimeSpan.Zero - tick), "default_requeue_delay"); AssertHelper.Throws <Exception>(() => c.Set("backoff_strategy", "invalid"), "backoff_strategy"); AssertHelper.Throws <Exception>(() => c.Set("max_backoff_duration", TimeSpan.Zero - tick), "max_backoff_duration"); AssertHelper.Throws <Exception>(() => c.Set("backoff_multiplier", TimeSpan.FromMilliseconds(1) - tick), "backoff_multiplier"); AssertHelper.Throws <Exception>(() => c.Set("max_attempts", 0 - 1), "max_attempts"); AssertHelper.Throws <Exception>(() => c.Set("low_rdy_idle_timeout", TimeSpan.FromSeconds(1) - tick), "low_rdy_idle_timeout"); AssertHelper.Throws <Exception>(() => c.Set("rdy_redistribute_interval", TimeSpan.FromMilliseconds(1) - tick), "rdy_redistribute_interval"); //c.Set("client_id", null); //c.Set("hostname", null); //c.Set("user_agent", null); //AssertHelper.Throws<Exception>(() => c.Set("heartbeat_interval", TimeSpan.MinValue - tick), "heartbeat_interval"); AssertHelper.Throws <Exception>(() => c.Set("sample_rate", 0 - 1), "sample_rate"); //c.Set("tls_config", null); //c.Set("deflate", false); AssertHelper.Throws <Exception>(() => c.Set("deflate_level", 1 - 1), "deflate_level"); //c.Set("snappy", false); //AssertHelper.Throws<Exception>(() => c.Set("output_buffer_size", Int64.MinValue - 1), ""); //AssertHelper.Throws<Exception>(() => c.Set("output_buffer_timeout", TimeSpan.MinValue - tick), "output_buffer_timeout"); AssertHelper.Throws <Exception>(() => c.Set("max_in_flight", 0 - 1), "max_in_flight"); AssertHelper.Throws <Exception>(() => c.Set("msg_timeout", TimeSpan.Zero - tick), "msg_timeout"); //AssertHelper.Throws<Exception>(() => c.Set("auth_secret", null), ""); }
public void TestProducerMultiPublishAsync() { var topicName = "multi_publish" + DateTime.Now.Unix(); const int msgCount = 10; var config = new NsqConfig(); var w = new Producer("127.0.0.1:4150", new ConsoleLogger(LogLevel.Debug), config); try { var testData = new List <byte[]>(); for (int i = 0; i < msgCount; i++) { testData.Add(Encoding.UTF8.GetBytes("multipublish_test_case")); } var responseTask = w.MultiPublishAsync(topicName, testData, "test0", 1); responseTask.Wait(); var trans = responseTask.Result; Assert.IsNull(trans.Error); Assert.IsNotNull(trans.Args); Assert.AreEqual(2, trans.Args.Length); Assert.AreEqual("test0", trans.Args[0]); Assert.AreEqual(1, trans.Args[1]); w.Publish(topicName, "bad_test_case"); readMessages(topicName, msgCount); } finally { w.Stop(); _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }
public void TestTls() { var c = new NsqConfig(); c.Set("tls_insecure_skip_verify", true); Assert.IsNotNull(c.TlsConfig, "TlsConfig"); Assert.IsTrue(c.TlsConfig.InsecureSkipVerify, "TlsConfig.InsecureSkipVerify"); // default Assert.IsTrue(c.TlsConfig.CheckCertificateRevocation); c.Set("tls-check-certificate-revocation", false); Assert.IsFalse(c.TlsConfig.CheckCertificateRevocation); c.Set("tls-check-certificate-revocation", true); Assert.IsTrue(c.TlsConfig.CheckCertificateRevocation); // tls_min_version c.Set("tls_min_version", "ssl3.0"); Assert.AreEqual(SslProtocols.Ssl3, c.TlsConfig.MinVersion); c.Set("tls_min_version", "tls1.0"); Assert.AreEqual(SslProtocols.Tls, c.TlsConfig.MinVersion); #if !NETFX_3_5 && !NETFX_4_0 c.Set("tls_min_version", "tls1.1"); Assert.AreEqual(SslProtocols.Tls11, c.TlsConfig.MinVersion); c.Set("tls_min_version", "tls1.2"); Assert.AreEqual(SslProtocols.Tls12, c.TlsConfig.MinVersion); #endif AssertHelper.Throws <Exception>(() => c.Set("tls_min_version", "ssl2.0")); AssertHelper.Throws <Exception>(() => c.Set("tls-min-version", "tls1.3")); }
public void TestConfigValidateDefault() { var c = new NsqConfig(); c.Validate(); }
public void TestConsumerBackoff() { var msgIDGood = Encoding.UTF8.GetBytes("1234567890asdfgh"); var msgGood = new Message(msgIDGood, Encoding.UTF8.GetBytes("good")); var msgIDBad = Encoding.UTF8.GetBytes("zxcvb67890asdfgh"); var msgBad = new Message(msgIDBad, Encoding.UTF8.GetBytes("bad")); var script = new[] { // SUB new instruction(0, FrameType.Response, "OK"), // IDENTIFY new instruction(0, FrameType.Response, "OK"), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgBad)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)), new instruction(20 * Time.Millisecond, FrameType.Message, frameMessage(msgGood)), // needed to exit test new instruction(200 * Time.Millisecond, -1, "exit") }; var n = new mockNSQD(script, IPAddress.Loopback); var topicName = "test_consumer_commands" + DateTime.Now.Unix(); var config = new NsqConfig(); config.MaxInFlight = 5; config.BackoffMultiplier = Time.Duration(10 * Time.Millisecond); var q = new Consumer(topicName, "ch", new ConsoleLogger(LogLevel.Debug), config); q.AddHandler(new testHandler()); q.ConnectToNsqd(n.tcpAddr); bool timeout = false; Select .CaseReceive(n.exitChan, o => { }) .CaseReceive(Time.After(TimeSpan.FromMilliseconds(5000)), o => { timeout = true; }) .NoDefault(); Assert.IsFalse(timeout, "timeout"); for (int i = 0; i < n.got.Count; i++) { Console.WriteLine("[{0}] {1}: {2}", n.gotTime[i].Formatted(), i, Encoding.UTF8.GetString(n.got[i])); } var expected = new[] { "IDENTIFY", "SUB " + topicName + " ch", "RDY 5", string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)), string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)), string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)), "RDY 5", "RDY 0", string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)), "RDY 1", "RDY 0", string.Format("REQ {0} 0", Encoding.UTF8.GetString(msgIDBad)), "RDY 1", "RDY 0", string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)), "RDY 1", "RDY 5", string.Format("FIN {0}", Encoding.UTF8.GetString(msgIDGood)), }; var actual = new List <string>(); foreach (var g in n.got) { actual.Add(Encoding.UTF8.GetString(g)); } q.DisconnectFromNsqd(n.tcpAddr); AssertHelper.AreEqual(expected, actual); }
// TODO: Deflate /*[Test, Ignore("Deflate not implemented")] * public void TestConsumerDeflate() * { * consumerTest(c => * { * c.Deflate = true; * }); * }*/ // TODO: Snappy /*[Test, Ignore("Snappy not implemented")] * public void TestConsumerSnappy() * { * consumerTest(c => * { * c.Snappy = true; * }); * }*/ // TODO: TLS/Deflate /*[Test, Ignore("TLS/Deflate not implemented")] * public void TestConsumerTLSDeflate() * { * consumerTest(c => * { * c.TlsV1 = true; * c.TlsConfig = new TlsConfig { InsecureSkipVerify = true }; * c.Deflate = true; * }); * }*/ // TODO: TLS/Snappy /*[Test, Ignore("TLS/Snappy not implemented")] * public void TestConsumerTLSSnappy() * { * consumerTest(c => * { * c.TlsV1 = true; * c.TlsConfig = new TlsConfig { InsecureSkipVerify = true }; * c.Snappy = true; * }); * }*/ /* * // TODO: Address when client certificate auth enabled * [TestMethod] * public void TestConsumerTLSClientCert() * { * consumerTest(c => * { * c.TlsConfig = new TlsConfig * { * //Certificates = cert * InsecureSkipVerify = true * }; * }); * } * * // TODO: Address when client certificate auth enabled * [TestMethod] * public void TestConsumerTLSClientCertViaSet() * { * consumerTest(c => * { * c.Set("ts_v1", true); * c.Set("tls_cert", "./test/client.pem"); * c.Set("tls_key", "./test/client.key"); * c.Set("tls_insecure_skip_verify", true); * }); * }*/ private void consumerTest(Action <NsqConfig> configSetter) { var config = new NsqConfig(); // so that the test can simulate reaching max requeues and a call to LogFailedMessage config.DefaultRequeueDelay = TimeSpan.Zero; // so that the test wont timeout from backing off config.MaxBackoffDuration = TimeSpan.FromMilliseconds(50); if (configSetter != null) { configSetter(config); } var topicName = "rdr_test"; // TODO: Deflate, Snappy /*if (config.Deflate) * topicName = topicName + "_deflate"; * else if (config.Snappy) * topicName = topicName + "_snappy";*/ if (config.TlsConfig != null) { topicName = topicName + "_tls"; } topicName = topicName + DateTime.Now.Unix(); try { var q = new Consumer(topicName, "ch", new TestConsoleLogger(), config); var h = new MyTestHandler { q = q }; q.AddHandler(h); SendMessage(topicName, "put", "{\"msg\":\"single\"}"); SendMessage(topicName, "mput", "{\"msg\":\"double\"}\n{\"msg\":\"double\"}"); SendMessage(topicName, "put", "TOBEFAILED"); h.messagesSent = 4; const string addr = "127.0.0.1:4150"; q.ConnectToNsqd(addr); var stats = q.GetStats(); Assert.AreNotEqual(0, stats.Connections, "stats report 0 connections (should be > 0)"); // NOTE: changed to just return without throwing; throwing Exceptions is a little more disruptive in .NET // than returning err in Go //AssertHelper.Throws<ErrAlreadyConnected>(() => q.ConnectToNSQD(addr), // "should not be able to connect to the same NSQ twice"); AssertHelper.Throws <ErrNotConnected>(() => q.DisconnectFromNsqd("1.2.3.4:4150"), "should not be able to disconnect from an unknown nsqd"); AssertHelper.Throws <TimeoutException>(() => q.ConnectToNsqd("1.2.3.4:4150"), "should not be able to connect to non-existent nsqd"); // should be able to disconnect from an nsqd q.DisconnectFromNsqd("1.2.3.4:4150"); q.DisconnectFromNsqd("127.0.0.1:4150"); q.Wait(); stats = q.GetStats(); Assert.AreEqual(h.messagesReceived + h.messagesFailed, stats.MessagesReceived, "stats report messages received"); Assert.AreEqual(8, h.messagesReceived, "messages received"); Assert.AreEqual(4, h.messagesSent, "messages sent"); Assert.AreEqual(1, h.messagesFailed, "failed messaged not done"); } finally { _nsqdHttpClient.DeleteTopic(topicName); _nsqLookupdHttpClient.DeleteTopic(topicName); } }