summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEPredicateTest.java
blob: 3fcb83bed0361377dceb56136903f5f8954d6975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.zookeeper.test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import org.apache.zookeeper.PortAssignment;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.server.quorum.FastLeaderElection;
import org.apache.zookeeper.server.quorum.QuorumPeer;
import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FLEPredicateTest extends ZKTestCase {

    protected static final Logger LOG = LoggerFactory.getLogger(FLEPredicateTest.class);

    class MockFLE extends FastLeaderElection {

        MockFLE(QuorumPeer peer) {
            super(peer, peer.createCnxnManager());
        }

        boolean predicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch) {
            return this.totalOrderPredicate(newId, newZxid, newEpoch, curId, curZxid, curEpoch);
        }

    }

    HashMap<Long, QuorumServer> peers;

    @Test
    public void testPredicate() throws IOException {

        peers = new HashMap<Long, QuorumServer>(3);

        /*
         * Creates list of peers.
         */
        for (int i = 0; i < 3; i++) {
            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
        }

        /*
         * Creating peer.
         */
        try {
            File tmpDir = ClientBase.createTmpDir();
            QuorumPeer peer = new QuorumPeer(peers, tmpDir, tmpDir, PortAssignment.unique(), 3, 0, 1000, 2, 2, 2);

            MockFLE mock = new MockFLE(peer);
            mock.start();

            /*
             * Lower epoch must return false
             */

            assertFalse(mock.predicate(4L, 0L, 0L, 3L, 0L, 2L));

            /*
             * Later epoch
             */
            assertTrue(mock.predicate(0L, 0L, 1L, 1L, 0L, 0L));

            /*
             * Higher zxid
             */
            assertTrue(mock.predicate(0L, 1L, 0L, 1L, 0L, 0L));

            /*
             * Higher id
             */
            assertTrue(mock.predicate(1L, 1L, 0L, 0L, 1L, 0L));
        } catch (IOException e) {
            LOG.error("Exception while creating quorum peer", e);
            fail("Exception while creating quorum peer");
        }
    }

}