Esempio n. 1
0
  @BeforeClass
  public static void init() {
    Config config = new Config();

    QueueConfig queueConfig = config.getQueueConfig(queueForTestQueueWithSizeLimit);
    queueConfig.setMaxSize(maxSizeForQueue);

    queueConfig = config.getQueueConfig(queueForTestOfferPoll);
    queueConfig.setMaxSize(maxSizeForQueue);

    server = Hazelcast.newHazelcastInstance(config);
    client = HazelcastClient.newHazelcastClient();
  }
 @Test
 public void testQueueConfig() {
   QueueConfig testQConfig = config.getQueueConfig("testQ");
   assertNotNull(testQConfig);
   assertEquals("testQ", testQConfig.getName());
   assertEquals(1000, testQConfig.getMaxSize());
   QueueConfig qConfig = config.getQueueConfig("q");
   assertNotNull(qConfig);
   assertEquals("q", qConfig.getName());
   assertEquals(2500, qConfig.getMaxSize());
   assertEquals(1, testQConfig.getItemListenerConfigs().size());
   ItemListenerConfig listenerConfig = testQConfig.getItemListenerConfigs().get(0);
   assertEquals("com.hazelcast.spring.DummyItemListener", listenerConfig.getClassName());
   assertTrue(listenerConfig.isIncludeValue());
 }
  @Test
  public void testPutInterruption() throws InterruptedException {
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "1000");
    config.getQueueConfig("default").setMaxSize(1);

    HazelcastInstance instance = createHazelcastInstance(config);
    final IQueue<Object> queue = instance.getQueue(randomName());
    final AtomicBoolean interrupted = new AtomicBoolean();

    assertTrue(queue.offer("item"));

    Thread t =
        new Thread() {
          public void run() {
            try {
              queue.put("item");
            } catch (InterruptedException e) {
              interrupted.set(true);
            }
          }
        };
    t.start();

    sleepSeconds(1);
    t.interrupt();
    t.join(5000);

    assertTrue(interrupted.get());
  }
 private HazelcastInstance[] createHazelcastInstances() {
   Config config = new Config();
   final String configName = randomString();
   config.getQueueConfig(configName).setMaxSize(100);
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   return factory.newInstances(config);
 }
Esempio n. 5
0
 @Test
 public void shouldCreateQueue() throws InterruptedException {
   // Given
   int numberOfEvents = 5;
   Config config = new Config();
   QueueStoreConfig jdbcBackedQueueConfig =
       QueueStoreConfigFactory.getJdbcBackedQueueConfig(dataSource(), queueName);
   QueueConfig messageQueue = config.getQueueConfig(queueName);
   messageQueue.setQueueStoreConfig(jdbcBackedQueueConfig);
   HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
   // When
   QueueStore storeImplementation = jdbcBackedQueueConfig.getStoreImplementation();
   for (long i = 0; i < numberOfEvents; i++) {
     storeImplementation.store(i, String.valueOf(i));
   }
   IQueue<String> iQueue = hazelcastInstance.getQueue(queueName);
   MatcherAssert.assertThat(iQueue.size(), CoreMatchers.equalTo(numberOfEvents));
   String actual = iQueue.take();
   MatcherAssert.assertThat(actual, CoreMatchers.equalTo("0"));
   hazelcastInstance.shutdown();
 }
 @Test
 public void testOfferLong() throws Exception {
   Config config = new Config();
   config.getQueueConfig("default").setMaxSize(200);
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances(config);
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   for (int i = 0; i < 100; i++) {
     assertTrue(q1.offer("item" + i, 100, TimeUnit.SECONDS));
     assertTrue(q2.offer("item" + i, 100, TimeUnit.SECONDS));
   }
   assertFalse(q1.offer("item"));
   assertFalse(q2.offer("item"));
   assertFalse(q1.offer("item", 2, TimeUnit.SECONDS));
   assertFalse(q2.offer("item", 2, TimeUnit.SECONDS));
   final CountDownLatch pollLatch = new CountDownLatch(200);
   new Thread(
           new Runnable() {
             public void run() {
               try {
                 Thread.sleep(3000);
                 for (int i = 0; i < 100; i++) {
                   if (("item" + i).equals(q1.poll(2, TimeUnit.SECONDS))) {
                     pollLatch.countDown();
                   }
                   if (("item" + i).equals(q2.poll(2, TimeUnit.SECONDS))) {
                     pollLatch.countDown();
                   }
                 }
               } catch (InterruptedException e) {
                 e.printStackTrace();
               }
             }
           })
       .start();
   assertOpenEventually(pollLatch);
   final ExecutorService es = Executors.newFixedThreadPool(50);
   final CountDownLatch latch = new CountDownLatch(200);
   for (int i = 0; i < 100; i++) {
     es.execute(
         new Runnable() {
           public void run() {
             try {
               if (q1.offer("item", 30, TimeUnit.SECONDS)) {
                 latch.countDown();
               }
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
           }
         });
     es.execute(
         new Runnable() {
           public void run() {
             try {
               if (q2.offer("item", 30, TimeUnit.SECONDS)) {
                 latch.countDown();
               }
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
           }
         });
   }
   assertOpenEventually(latch);
   es.shutdown();
 }
 protected Config createConfig() {
   Config config = new Config();
   QueueConfig queueConfig = config.getQueueConfig(queueName);
   queueConfig.setMaxSize(6);
   return config;
 }