Search in sources :

Example 6 with UsageCount

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

the class ServiceRegistryTest method testFlushUsageCountRetry.

@SuppressWarnings("unchecked")
public void testFlushUsageCountRetry() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    final Bundle b = Mockito.mock(Bundle.class);
    final ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    final UsageCount uc = new UsageCount(ref, false);
    final ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    final UsageCount uc2 = new UsageCount(ref2, false);
    final ConcurrentMap<Bundle, UsageCount[]> orgInUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    final ConcurrentMap<Bundle, UsageCount[]> inUseMap = Mockito.mock(ConcurrentMap.class, AdditionalAnswers.delegatesTo(orgInUseMap));
    Mockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            inUseMap.put(b, new UsageCount[] { uc });
            return false;
        }
    }).when(inUseMap).replace(Mockito.isA(Bundle.class), Mockito.isA(UsageCount[].class), Mockito.isA(UsageCount[].class));
    setPrivateField(sr, "m_inUseMap", inUseMap);
    inUseMap.put(b, new UsageCount[] { uc, uc2 });
    sr.flushUsageCount(b, null, uc);
    assertNull("A 'concurrent' process has removed uc2 as well, " + "so the entry for 'b' should have been removed", inUseMap.get(b));
}
Also used : Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 7 with UsageCount

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

the class ServiceRegistryTest method testObtainUsageCountPrototypeUnknownLookup.

public void testObtainUsageCountPrototypeUnknownLookup() 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, true);
    ServiceHolder sh = new ServiceHolder();
    String svc = "foobar";
    sh.m_service = svc;
    uc.m_svcHolderRef.set(sh);
    inUseMap.put(b, new UsageCount[] { uc });
    assertNull(sr.obtainUsageCount(b, Mockito.mock(ServiceReference.class), null, null));
    UsageCount uc2 = sr.obtainUsageCount(b, ref, svc, null);
    assertSame(uc, uc2);
}
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)

Example 8 with UsageCount

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

the class ServiceRegistryTest method testFlushUsageCountNullRef.

public void testFlushUsageCountNullRef() 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);
    Bundle b2 = 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);
    ServiceReference<?> ref3 = Mockito.mock(ServiceReference.class);
    UsageCount uc3 = new UsageCount(ref3, true);
    inUseMap.put(b, new UsageCount[] { uc2, uc });
    inUseMap.put(b2, new UsageCount[] { uc3 });
    assertEquals("Precondition", 2, inUseMap.size());
    sr.flushUsageCount(b, null, uc);
    assertEquals(2, inUseMap.size());
    sr.flushUsageCount(b, null, uc2);
    assertEquals(1, inUseMap.size());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 9 with UsageCount

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

the class ServiceRegistryTest method testUsageCountCleanup.

public void testUsageCountCleanup() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle regBundle = Mockito.mock(Bundle.class);
    ServiceRegistration<?> reg = sr.registerService(regBundle, new String[] { String.class.getName() }, "hi", null);
    final Bundle clientBundle = Mockito.mock(Bundle.class);
    Mockito.when(clientBundle.getBundleId()).thenReturn(327L);
    assertEquals("hi", sr.getService(clientBundle, reg.getReference(), false));
    sr.ungetService(clientBundle, reg.getReference(), null);
    ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    sr.unregisterService(regBundle, reg);
    assertEquals(0, inUseMap.size());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 10 with UsageCount

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

the class ServiceRegistryTest method testFlushUsageCountRetry2.

public void testFlushUsageCountRetry2() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    final Bundle b = Mockito.mock(Bundle.class);
    final ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    final UsageCount uc = new UsageCount(ref, false);
    final ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    final UsageCount uc2 = new UsageCount(ref2, false);
    final ConcurrentMap<Bundle, UsageCount[]> orgInUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    final ConcurrentMap<Bundle, UsageCount[]> inUseMap = Mockito.mock(ConcurrentMap.class, AdditionalAnswers.delegatesTo(orgInUseMap));
    Mockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            inUseMap.put(b, new UsageCount[] { uc, uc2 });
            return false;
        }
    }).when(inUseMap).remove(Mockito.isA(Bundle.class), Mockito.isA(UsageCount[].class));
    setPrivateField(sr, "m_inUseMap", inUseMap);
    inUseMap.put(b, new UsageCount[] { uc });
    sr.flushUsageCount(b, null, uc);
    assertEquals(1, inUseMap.get(b).length);
    assertSame(uc2, inUseMap.get(b)[0]);
}
Also used : Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

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