Search in sources :

Example 6 with ServiceHolder

use of org.apache.felix.framework.ServiceRegistry.ServiceHolder 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 7 with ServiceHolder

use of org.apache.felix.framework.ServiceRegistry.ServiceHolder 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)

Aggregations

ServiceHolder (org.apache.felix.framework.ServiceRegistry.ServiceHolder)7 UsageCount (org.apache.felix.framework.ServiceRegistry.UsageCount)7 Bundle (org.osgi.framework.Bundle)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)6 ServiceReferenceImpl (org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1