예제 #1
0
 public void testGetAfterRemoveReturnsNull() {
   final String testKey = "hefty";
   final Object testValue = new Object();
   context.put(testKey, testValue);
   context.remove(testKey);
   assertNull("remove() should result in null value for get()", context.get(testKey));
 }
 @Test
 public void shouldRemoveAnEntryFromTheMapWhenAutoClosed() throws Exception {
   try (final CloseableThreadContext ignored = CloseableThreadContext.put(key, value)) {
     assertThat(ThreadContext.get(key), is(value));
   }
   assertThat(ThreadContext.containsKey(key), is(false));
 }
 @Test
 public void shouldPushAndPopAnEntryToTheStack() throws Exception {
   final String message = "message";
   try (final CloseableThreadContext ignored = CloseableThreadContext.push(message)) {
     assertThat(ThreadContext.peek(), is(message));
   }
   assertThat(ThreadContext.peek(), is(""));
 }
 @Test
 public void shouldUseAnEmptyStringIfNoValueIsSupplied() throws Exception {
   final String key2 = "key2";
   try (final CloseableThreadContext ignored = CloseableThreadContext.put(key, value, key2)) {
     assertThat(ThreadContext.get(key), is(value));
     assertThat(ThreadContext.get(key2), is(""));
   }
 }
 @Test
 public void shouldPreserveOldEntriesFromTheMapWhenAutoClosed() throws Exception {
   final String oldValue = "oldValue";
   ThreadContext.put(key, oldValue);
   try (final CloseableThreadContext ignored = CloseableThreadContext.put(key, value)) {
     assertThat(ThreadContext.get(key), is(value));
   }
   assertThat(ThreadContext.get(key), is(oldValue));
 }
 @Test
 public void shouldAddTwoEntriesToTheMap() throws Exception {
   final String key2 = "key2";
   final String value2 = "value2";
   try (final CloseableThreadContext ignored =
       CloseableThreadContext.put(key, value, key2, value2)) {
     assertThat(ThreadContext.get(key), is(value));
     assertThat(ThreadContext.get(key2), is(value2));
   }
 }
 @Test
 public void shouldPushAndPopAParameterizedEntryToTheStack() throws Exception {
   final String parameterizedMessage = "message {}";
   final String parameterizedMessageParameter = "param";
   final String formattedMessage =
       parameterizedMessage.replace("{}", parameterizedMessageParameter);
   try (final CloseableThreadContext ignored =
       CloseableThreadContext.push(parameterizedMessage, parameterizedMessageParameter)) {
     assertThat(ThreadContext.peek(), is(formattedMessage));
   }
   assertThat(ThreadContext.peek(), is(""));
 }
 @Test
 public void shouldNestEntries() throws Exception {
   final String oldValue = "oldValue";
   final String innerValue = "innerValue";
   ThreadContext.put(key, oldValue);
   try (final CloseableThreadContext ignored = CloseableThreadContext.put(key, value)) {
     assertThat(ThreadContext.get(key), is(value));
     try (final CloseableThreadContext ignored2 = CloseableThreadContext.put(key, innerValue)) {
       assertThat(ThreadContext.get(key), is(innerValue));
     }
     assertThat(ThreadContext.get(key), is(value));
   }
   assertThat(ThreadContext.get(key), is(oldValue));
 }
예제 #9
0
          public RecorderHolder initialValue() {
            final ThreadContext threadContext = m_threadContextLocator.get();

            if (threadContext == null) {
              throw new UncheckedException("Only Worker Threads can invoke tests");
            }

            final TestRecorder recorder =
                new TestRecorder(
                    threadContext.getDispatchResultReporter(),
                    new StopWatchImplementation(m_timeAuthority));

            return new RecorderHolder(threadContext, recorder);
          }
예제 #10
0
 public void end(boolean success) {
   if (--m_nestingDepth == 0) {
     // Leaving outer frame.
     m_recorder.end(success);
     m_threadContext.popDispatchContext();
   }
 }
예제 #11
0
 public void start() throws DispatchStateException {
   if (m_nestingDepth++ == 0) {
     // Entering outer frame.
     m_threadContext.pushDispatchContext(m_recorder);
     m_recorder.start();
   }
 }
예제 #12
0
 public void execute(final Runnable command) {
   ThreadContext.runInContext(
       context.get(),
       new Runnable() {
         public void run() {
           filters.get().execute(command);
         }
       });
 }
예제 #13
0
  public void testClearRemovesMDCValue() {

    ThreadContext log4jThreadContext = ThreadContext.newMDCEnabledContext();

    Hashtable mdcContext = MDC.getContext();
    assertNull(mdcContext);

    final String testKey = "painter";
    log4jThreadContext.put(testKey, 7);

    mdcContext = MDC.getContext();
    assertEquals(1, mdcContext.size());

    log4jThreadContext.clear();

    assertEquals(0, mdcContext.size());

    assertNull("get() after clear should return null", log4jThreadContext.get(testKey));
  }
예제 #14
0
  /** Create a new callframe and arg list to add an invokee argument at the front. */
  public CallSiteDescriptor injectInvokee(
      ThreadContext tc, Object[] oldArgs, SixModelObject invokee) {
    Object[] newArgs = new Object[oldArgs.length + 1];
    System.arraycopy(oldArgs, 0, newArgs, 1, oldArgs.length);
    newArgs[0] = invokee;

    byte[] newFlags = new byte[argFlags.length + 1];
    System.arraycopy(argFlags, 0, newFlags, 1, argFlags.length);
    newFlags[0] = ARG_OBJ;

    tc.flatArgs = newArgs;
    return new CallSiteDescriptor(newFlags, names);
  }
예제 #15
0
 public void restart() {
   synchronized (lifecycleLock) {
     ThreadContext.get().setCurrentFactory(factory);
     fireLifecycleEvent(RESTARTING);
     paused.set(true);
     final Node node = factory.node;
     final ILogger logger = getLogger();
     List<Record> lsOwnedRecords = new ArrayList<Record>();
     for (CMap cmap : node.concurrentMapManager.getCMaps().values()) {
       if (cmap.isUserMap()) {
         lsOwnedRecords.addAll(cmap.getMapIndexService().getOwnedRecords());
       }
     }
     node.onRestart();
     node.clientHandlerService.restart();
     node.connectionManager.onRestart();
     node.clusterManager.onRestart();
     node.concurrentMapManager.onRestart();
     node.rejoin();
     final CountDownLatch latch = new CountDownLatch(lsOwnedRecords.size());
     final ParallelExecutor executor = node.executorManager.newParallelExecutor(16);
     for (final Record ownedRecord : lsOwnedRecords) {
       executor.execute(
           new Runnable() {
             public void run() {
               try {
                 ConcurrentMapManager.MPut mput = node.concurrentMapManager.new MPut();
                 mput.merge(ownedRecord);
                 // invalidate record now (skipped invalidation on restart)
                 ownedRecord.invalidate();
                 latch.countDown();
               } catch (Exception e) {
                 logger.log(Level.WARNING, e.getMessage(), e);
               }
             }
           });
     }
     try {
       latch.await(60, TimeUnit.SECONDS);
     } catch (InterruptedException ignored) {
     }
     paused.set(false);
     fireLifecycleEvent(RESTARTED);
   }
 }
예제 #16
0
 public void testCanAccessOtherThreadsContext() throws InterruptedException {
   final String testKey = "vanity";
   final Object testValue = new Object();
   final CountDownLatch finished = new CountDownLatch(1);
   final Thread otherThread =
       new Thread(
           new Runnable() {
             public void run() {
               context.put(testKey, testValue);
               finished.countDown();
             }
           });
   otherThread.start();
   finished.await();
   Map<String, Object> forThread = context.forThread(otherThread);
   assertEquals(1, forThread.size());
   assertEquals(testValue, forThread.get(testKey));
 }
 @Before
 public void setUp() throws Exception {
   ThreadContext.clearAll();
 }
예제 #18
0
 public void testGetsEmptyContextForUnknownThread() {
   assertEquals(0, context.forThread(new Thread()).size());
 }
예제 #19
0
 public void testClearRemovesValue() {
   final String testKey = "painter";
   context.put(testKey, 7);
   context.clear();
   assertNull("get() after clear should return null", context.get(testKey));
 }
 @Test
 public void shouldAddAnEntryToTheMap() throws Exception {
   try (final CloseableThreadContext ignored = CloseableThreadContext.put(key, value)) {
     assertThat(ThreadContext.get(key), is(value));
   }
 }
예제 #21
0
 public void testGetRetrievesPutValue() {
   final String testKey = "brainy";
   final Object testValue = new Object();
   context.put(testKey, testValue);
   assertEquals(testValue, context.get(testKey));
 }
예제 #22
0
 public ExecutionScope(final String name) {
   // Add this IScope to the stack of nested IScopes.
   ThreadContext.addIScope(this, name);
 }
예제 #23
0
 @Override
 public void close() {
   // Remove this IScope from the stack of nested IScopes.
   ThreadContext.removeIScope(this);
 }
예제 #24
0
 public void testGetOfUnusedKeyReturnsNull() {
   final String testKey = "handy";
   assertNull("get() of unused key should return null", context.get(testKey));
 }