### Question:
Producer { public void publish(final String message) { publish(message, 0); } Producer(final Jedis jedis, final String topic); void publish(final String message); void clean(); void publish(String message, int seconds);  }### Answer:
@Test public void publishAndConsume() { Producer p = new Producer(new Jedis("localhost"), "foo"); Consumer c = new Consumer(new Jedis("localhost"), "a subscriber", "foo"); p.publish("hello world!"); assertEquals("hello world!", c.consume()); }
@Test public void publishAndRead() { Producer p = new Producer(new Jedis("localhost"), "foo"); Consumer c = new Consumer(new Jedis("localhost"), "a subscriber", "foo"); p.publish("hello world!"); assertEquals("hello world!", c.read()); assertEquals("hello world!", c.read()); }
@Test public void unreadMessages() { Producer p = new Producer(new Jedis("localhost"), "foo"); Consumer c = new Consumer(new Jedis("localhost"), "a subscriber", "foo"); assertEquals(0, c.unreadMessages()); p.publish("hello world!"); assertEquals(1, c.unreadMessages()); p.publish("hello world!"); assertEquals(2, c.unreadMessages()); c.consume(); assertEquals(1, c.unreadMessages()); }
@Test public void raceConditionsWhenPublishing() throws InterruptedException { Producer slow = new SlowProducer(new Jedis("localhost"), "foo"); Consumer c = new Consumer(new Jedis("localhost"), "a subscriber", "foo"); slow.publish("a"); Thread t = new Thread(new Runnable() { @Override public void run() { Producer fast = new Producer(new Jedis("localhost"), "foo"); fast.publish("b"); } }); t.start(); t.join(); assertEquals("a", c.consume()); assertEquals("b", c.consume()); }
@Test public void expiredMessages() throws InterruptedException { Consumer c = new SlowConsumer(new Jedis("localhost"), "a consumer", "foo", 2000L); Producer p = new Producer(new Jedis("localhost"), "foo"); p.publish("un mensaje", 1); assertNull(c.consume()); }
@Test public void firstMessageExpired() throws InterruptedException { Consumer c = new SlowConsumer(new Jedis("localhost"), "a consumer", "foo", 2000L); Producer p = new Producer(new Jedis("localhost"), "foo"); p.publish("1", 1); p.publish("2", 0); assertEquals("2", c.consume()); }