Search in sources :

Example 11 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method sendsEventsIntoEventBus.

@Test
void sendsEventsIntoEventBus() {
    Core core = mock(Core.class);
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(NodeStateChangedEvent.class));
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    try {
        Node node = new Node(ctx, mock(NodeIdentifier.class), NO_ALTERNATE) {

            @Override
            protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
                Service s = mock(Service.class);
                when(s.type()).thenReturn(serviceType);
                when(s.states()).thenReturn(DirectProcessor.create());
                when(s.state()).thenReturn(ServiceState.IDLE);
                return s;
            }
        };
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.removeService(ServiceType.KV, Optional.of("bucket")).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.disconnect().block();
        node.disconnect().block();
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        List<Event> events = eventBus.publishedEvents();
        assertTrue(events.remove(0) instanceof NodeConnectedEvent);
        assertTrue(events.get(0) instanceof ServiceAddedEvent);
        assertTrue(events.get(1) instanceof ServiceAddedEvent);
        assertTrue(events.get(2) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(3) instanceof ServiceRemovedEvent);
        assertTrue(events.get(4) instanceof ServiceRemovedEvent);
        assertTrue(events.get(5) instanceof ServiceRemoveIgnoredEvent);
        assertTrue(events.get(6) instanceof NodeDisconnectedEvent);
        assertTrue(events.get(7) instanceof NodeDisconnectIgnoredEvent);
        assertTrue(events.get(8) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(9) instanceof ServiceRemoveIgnoredEvent);
    } finally {
        env.shutdown();
    }
}
Also used : ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) CoreContext(com.couchbase.client.core.CoreContext) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) Optional(java.util.Optional) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) Service(com.couchbase.client.core.service.Service) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) ServiceType(com.couchbase.client.core.service.ServiceType) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) Event(com.couchbase.client.core.cnc.Event) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 12 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method performsDisconnect.

@Test
void performsDisconnect() {
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            Service s = mock(Service.class);
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }
    };
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.KV));
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
    assertTrue(node.serviceEnabled(ServiceType.KV));
    assertTrue(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.CONNECTED, node.state());
    node.disconnect().block();
    assertFalse(node.serviceEnabled(ServiceType.KV));
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.DISCONNECTED, node.state());
    node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.DISCONNECTED, node.state());
}
Also used : Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) Test(org.junit.jupiter.api.Test)

Example 13 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method connectingIfAllConnecting.

@Test
void connectingIfAllConnecting() {
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            Service s = mock(Service.class);
            when(s.state()).thenReturn(ServiceState.CONNECTING);
            when(s.states()).thenReturn(DirectProcessor.create());
            return s;
        }
    };
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.KV));
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    assertTrue(node.serviceEnabled(ServiceType.KV));
    assertFalse(node.serviceEnabled(ServiceType.QUERY));
    node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
    assertTrue(node.serviceEnabled(ServiceType.QUERY));
    assertEquals(NodeState.CONNECTING, node.state());
}
Also used : Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) Test(org.junit.jupiter.api.Test)

Example 14 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method sendsToFoundLocalService.

@Test
@SuppressWarnings({ "unchecked" })
void sendsToFoundLocalService() {
    final Service s = mock(Service.class);
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }
    };
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    KeyValueRequest r = mock(KeyValueRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.KV);
    when(r.bucket()).thenReturn("bucket");
    when(r.context()).thenReturn(new RequestContext(CTX, r));
    node.send(r);
    verify(s, times(1)).send(eq(r));
}
Also used : KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Example 15 with Service

use of com.couchbase.client.core.service.Service in project couchbase-jvm-clients by couchbase.

the class NodeTest method connectedIfOneConnected.

@Test
void connectedIfOneConnected() {
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            Service s = mock(Service.class);
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            return s;
        }
    };
    assertEquals(NodeState.DISCONNECTED, node.state());
    assertFalse(node.serviceEnabled(ServiceType.KV));
    node.addService(ServiceType.KV, 1234, Optional.of("bucket")).block();
    assertTrue(node.serviceEnabled(ServiceType.KV));
    assertEquals(NodeState.CONNECTED, node.state());
}
Also used : Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) Test(org.junit.jupiter.api.Test)

Aggregations

Service (com.couchbase.client.core.service.Service)18 ServiceType (com.couchbase.client.core.service.ServiceType)18 Optional (java.util.Optional)15 Test (org.junit.jupiter.api.Test)15 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)3 QueryRequest (com.couchbase.client.core.msg.query.QueryRequest)3 AnalyticsService (com.couchbase.client.core.service.AnalyticsService)3 BackupService (com.couchbase.client.core.service.BackupService)3 EventingService (com.couchbase.client.core.service.EventingService)3 KeyValueService (com.couchbase.client.core.service.KeyValueService)3 ManagerService (com.couchbase.client.core.service.ManagerService)3 QueryService (com.couchbase.client.core.service.QueryService)3 SearchService (com.couchbase.client.core.service.SearchService)3 ViewService (com.couchbase.client.core.service.ViewService)3 ServiceAddIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent)2 ServiceAddedEvent (com.couchbase.client.core.cnc.events.service.ServiceAddedEvent)2 ServiceRemoveIgnoredEvent (com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent)2 ServiceRemovedEvent (com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent)2 Request (com.couchbase.client.core.msg.Request)2 RequestContext (com.couchbase.client.core.msg.RequestContext)2