Search in sources :

Example 16 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testObtainUsageCountRetry1.

@SuppressWarnings("unchecked")
public void testObtainUsageCountRetry1() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    final Bundle b = Mockito.mock(Bundle.class);
    final ConcurrentMap<Bundle, UsageCount[]> orgInUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    ConcurrentMap<Bundle, UsageCount[]> inUseMap = Mockito.mock(ConcurrentMap.class, AdditionalAnswers.delegatesTo(orgInUseMap));
    Mockito.doAnswer(new Answer<UsageCount[]>() {

        @Override
        public UsageCount[] answer(InvocationOnMock invocation) throws Throwable {
            // This mimicks another thread putting another UsageCount in concurrently
            // The putIfAbsent() will fail and it has to retry
            UsageCount uc = new UsageCount(Mockito.mock(ServiceReference.class), false);
            UsageCount[] uca = new UsageCount[] { uc };
            orgInUseMap.put(b, uca);
            return uca;
        }
    }).when(inUseMap).putIfAbsent(Mockito.any(Bundle.class), Mockito.any(UsageCount[].class));
    setPrivateField(sr, "m_inUseMap", inUseMap);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    assertEquals(0, orgInUseMap.size());
    UsageCount uc = sr.obtainUsageCount(b, ref, null, false);
    assertEquals(1, orgInUseMap.size());
    assertEquals(2, orgInUseMap.get(b).length);
    assertSame(ref, uc.m_ref);
    assertFalse(uc.m_prototype);
    List<UsageCount> l = new ArrayList<UsageCount>(Arrays.asList(orgInUseMap.get(b)));
    l.remove(uc);
    assertEquals("There should be one UsageCount left", 1, l.size());
    assertNotSame(ref, l.get(0).m_ref);
}
Also used : Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConcurrentMap(java.util.concurrent.ConcurrentMap) ArrayList(java.util.ArrayList) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 17 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testUngetService.

@SuppressWarnings("unchecked")
public void testUngetService() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
    Mockito.when(ref.getRegistration()).thenReturn(reg);
    final ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    UsageCount uc = new UsageCount(ref, false);
    uc.m_svcHolderRef.set(new ServiceHolder());
    inUseMap.put(b, new UsageCount[] { uc });
    assertFalse(sr.ungetService(b, ref, null));
    assertNull(uc.m_svcHolderRef.get());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServiceHolder(org.apache.felix.framework.ServiceRegistry.ServiceHolder) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 18 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testGetServiceHolderAwait.

@SuppressWarnings("unchecked")
public void testGetServiceHolderAwait() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    final String svc = "test";
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    Mockito.when(reg.isValid()).thenReturn(true);
    ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
    Mockito.when(ref.getRegistration()).thenReturn(reg);
    UsageCount uc = sr.obtainUsageCount(b, ref, null, false);
    // Set an empty Service Holder so we can test that it waits.
    final ServiceHolder sh = new ServiceHolder();
    uc.m_svcHolderRef.set(sh);
    final StringBuffer sb = new StringBuffer();
    final AtomicBoolean threadException = new AtomicBoolean(false);
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
            }
            sh.m_service = svc;
            if (sb.length() > 0) {
                // Should not have put anything in SB until countDown() was called...
                threadException.set(true);
            }
            sh.m_latch.countDown();
        }
    };
    assertFalse(t.isInterrupted());
    t.start();
    Object actualSvc = sr.getService(b, ref, false);
    sb.append(actualSvc);
    t.join();
    assertFalse("This thread did not wait until the latch was count down", threadException.get());
    assertSame(svc, actualSvc);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Bundle(org.osgi.framework.Bundle) ServiceHolder(org.apache.felix.framework.ServiceRegistry.ServiceHolder) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 19 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testObtainUsageCountPrototypeUnknownLookup2.

public void testObtainUsageCountPrototypeUnknownLookup2() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    @SuppressWarnings("unchecked") ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    Bundle b = Mockito.mock(Bundle.class);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    UsageCount uc = new UsageCount(ref, false);
    inUseMap.put(b, new UsageCount[] { uc });
    assertNull(sr.obtainUsageCount(b, Mockito.mock(ServiceReference.class), null, null));
    UsageCount uc2 = sr.obtainUsageCount(b, ref, null, null);
    assertSame(uc, uc2);
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 20 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testFlushUsageCount.

public void testFlushUsageCount() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    @SuppressWarnings("unchecked") ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    Bundle b = Mockito.mock(Bundle.class);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    UsageCount uc = new UsageCount(ref, false);
    ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    UsageCount uc2 = new UsageCount(ref2, true);
    inUseMap.put(b, new UsageCount[] { uc, uc2 });
    assertEquals("Precondition", 1, inUseMap.size());
    assertEquals("Precondition", 2, inUseMap.values().iterator().next().length);
    sr.flushUsageCount(b, ref, uc);
    assertEquals(1, inUseMap.size());
    assertEquals(1, inUseMap.values().iterator().next().length);
    assertSame(uc2, inUseMap.values().iterator().next()[0]);
    sr.flushUsageCount(b, ref2, uc2);
    assertEquals(0, inUseMap.size());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Aggregations

UsageCount (org.apache.felix.framework.ServiceRegistry.UsageCount)20 Bundle (org.osgi.framework.Bundle)20 ConcurrentMap (java.util.concurrent.ConcurrentMap)19 ServiceReferenceImpl (org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)7 ServiceHolder (org.apache.felix.framework.ServiceRegistry.ServiceHolder)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ArrayList (java.util.ArrayList)1 ServiceReference (org.osgi.framework.ServiceReference)1