Search in sources :

Example 1 with Supplier

use of com.twitter.common.base.Supplier in project commons by twitter.

the class GroupTest method testUpdateMembershipData.

@Test
public void testUpdateMembershipData() throws Exception {
    Supplier<byte[]> dataSupplier = new EasyMockTest.Clazz<Supplier<byte[]>>() {
    }.createMock();
    byte[] initial = "start".getBytes();
    expect(dataSupplier.get()).andReturn(initial);
    byte[] second = "update".getBytes();
    expect(dataSupplier.get()).andReturn(second);
    replay(dataSupplier);
    Membership membership = joinGroup.join(dataSupplier, onLoseMembership);
    assertArrayEquals("Initial setting is incorrect.", initial, zkClient.get().getData(membership.getMemberPath(), false, null));
    assertArrayEquals("Updating supplier should not change membership data", initial, zkClient.get().getData(membership.getMemberPath(), false, null));
    membership.updateMemberData();
    assertArrayEquals("Updating membership should change data", second, zkClient.get().getData(membership.getMemberPath(), false, null));
    verify(dataSupplier);
}
Also used : EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Membership(com.twitter.common.zookeeper.Group.Membership) Supplier(com.twitter.common.base.Supplier) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with Supplier

use of com.twitter.common.base.Supplier in project commons by twitter.

the class ServerSetImpl method join.

private EndpointStatus join(InetSocketAddress endpoint, Map<String, InetSocketAddress> additionalEndpoints, Optional<Integer> shardId) throws JoinException, InterruptedException {
    checkNotNull(endpoint);
    checkNotNull(additionalEndpoints);
    final MemberStatus memberStatus = new MemberStatus(endpoint, additionalEndpoints, shardId);
    Supplier<byte[]> serviceInstanceSupplier = new Supplier<byte[]>() {

        @Override
        public byte[] get() {
            return memberStatus.serializeServiceInstance();
        }
    };
    final Membership membership = group.join(serviceInstanceSupplier);
    return new EndpointStatus() {

        @Override
        public void update(Status status) throws UpdateException {
            checkNotNull(status);
            LOG.warning("This method is deprecated. Please use leave() instead.");
            if (status == Status.DEAD) {
                leave();
            } else {
                LOG.warning("Status update has been ignored");
            }
        }

        @Override
        public void leave() throws UpdateException {
            memberStatus.leave(membership);
        }
    };
}
Also used : Status(com.twitter.thrift.Status) Membership(com.twitter.common.zookeeper.Group.Membership) Supplier(com.twitter.common.base.Supplier)

Aggregations

Supplier (com.twitter.common.base.Supplier)2 Membership (com.twitter.common.zookeeper.Group.Membership)2 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)1 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)1 Status (com.twitter.thrift.Status)1 Test (org.junit.Test)1