Search in sources :

Example 11 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testUngetServiceThreadMarking.

public void testUngetServiceThreadMarking() {
    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);
    assertFalse("There is no usage count, so this method should return false", sr.ungetService(b, ref, null));
    InOrder inOrder = Mockito.inOrder(reg);
    inOrder.verify(reg, Mockito.times(1)).currentThreadMarked();
    inOrder.verify(reg, Mockito.times(1)).markCurrentThread();
    inOrder.verify(reg, Mockito.times(1)).unmarkCurrentThread();
}
Also used : InOrder(org.mockito.InOrder) Bundle(org.osgi.framework.Bundle) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 12 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl 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 13 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl 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

ServiceReferenceImpl (org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)13 Bundle (org.osgi.framework.Bundle)13 UsageCount (org.apache.felix.framework.ServiceRegistry.UsageCount)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)6 ServiceHolder (org.apache.felix.framework.ServiceRegistry.ServiceHolder)6 InOrder (org.mockito.InOrder)2 ServiceException (org.osgi.framework.ServiceException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1