PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/test/java/com/hazelcast/impl/RedoNoneMemberTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 197 lines | 159 code | 23 blank | 15 comment | 0 complexity | c15703d0067b941fd96d2ab27ce0e891 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.impl;
 18
 19import com.hazelcast.config.Config;
 20import com.hazelcast.core.Hazelcast;
 21import com.hazelcast.core.HazelcastInstance;
 22import com.hazelcast.nio.Address;
 23import org.junit.After;
 24import org.junit.BeforeClass;
 25import org.junit.Ignore;
 26import org.junit.Test;
 27import org.junit.runner.RunWith;
 28
 29import java.util.LinkedList;
 30import java.util.List;
 31
 32import static junit.framework.Assert.assertEquals;
 33
 34@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 35public class RedoNoneMemberTest extends RedoTestService {
 36
 37    @BeforeClass
 38    public static void init() throws Exception {
 39        System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1");
 40        Hazelcast.shutdownAll();
 41    }
 42
 43    @After
 44    public void cleanup() throws Exception {
 45        Hazelcast.shutdownAll();
 46    }
 47
 48    @Test(timeout = 100000)
 49    public void testMultiCallToNoneMember() throws Exception {
 50        Config config = new Config();
 51        config.setProperty(GroupProperties.PROP_PARTITION_TABLE_SEND_INTERVAL, "2");
 52        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
 53        RedoTestService.BeforeAfterTester t = new BeforeAfterTester(new NoneMemberBehavior(h1), new MultiCallBuilder(h1));
 54        t.run();
 55    }
 56
 57    @Test(timeout = 100000)
 58    public void testMapRemoteTargetNotMember() {
 59        Config config = new Config();
 60        config.setProperty(GroupProperties.PROP_PARTITION_TABLE_SEND_INTERVAL, "2");
 61        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
 62        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
 63        new RunAfterTester(new TargetNotMemberBehavior(h1, h2), new KeyCallBuilder(h1)).run();
 64        new RunAfterTester(new TargetNotMemberBehavior(h2, h1), new KeyCallBuilder(h2)).run();
 65    }
 66
 67    class TargetNotMemberBehavior extends BeforeAfterBehavior {
 68        final HazelcastInstance caller;
 69        final HazelcastInstance target;
 70        final Node callerNode;
 71        final Node targetNode;
 72
 73        TargetNotMemberBehavior(HazelcastInstance caller, HazelcastInstance target) {
 74            this.caller = caller;
 75            this.target = target;
 76            this.callerNode = getNode(caller);
 77            this.targetNode = getNode(target);
 78        }
 79
 80        @Override
 81        void before() throws Exception {
 82            callerNode.clusterManager.enqueueAndWait(new Processable() {
 83                public void process() {
 84                    callerNode.clusterManager.removeMember((MemberImpl) target.getCluster().getLocalMember());
 85                    assertEquals(1, callerNode.clusterManager.lsMembers.size());
 86                }
 87            }, 5);
 88        }
 89
 90        @Override
 91        void after() {
 92            callerNode.clusterManager.enqueueAndWait(new Processable() {
 93                public void process() {
 94                    MemberImpl secondMember = new MemberImpl(((MemberImpl) target.getCluster().getLocalMember()).getAddress(), false);
 95                    callerNode.clusterManager.addMember(secondMember);
 96                    assertEquals(2, callerNode.clusterManager.lsMembers.size());
 97                }
 98            }, 5);
 99        }
100    }
101
102    @Test(timeout = 100000)
103    public void testQueueRemoteCallerNoneMember() {
104        Config config = new Config();
105        config.setProperty(GroupProperties.PROP_PARTITION_TABLE_SEND_INTERVAL, "2");
106        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
107        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
108        CallBuilder calls = new QueueCallBuilder(h2);
109        final Node node1 = getNode(h1);
110        BeforeAfterBehavior behavior = new BeforeAfterBehavior() {
111            public void before() throws Exception {
112                node1.clusterManager.enqueueAndWait(new Processable() {
113                    public void process() {
114                        node1.clusterManager.removeMember((MemberImpl) h2.getCluster().getLocalMember());
115                        assertEquals(1, node1.clusterManager.lsMembers.size());
116                    }
117                }, 5);
118            }
119
120            public void after() {
121                node1.clusterManager.enqueueAndWait(new Processable() {
122                    public void process() {
123                        MemberImpl secondMember = new MemberImpl(((MemberImpl) h2.getCluster().getLocalMember()).getAddress(), false);
124                        node1.clusterManager.addMember(secondMember);
125                        assertEquals(2, node1.clusterManager.lsMembers.size());
126                    }
127                }, 5);
128            }
129
130            public void destroy() {
131            }
132        };
133        BeforeAfterTester t = new BeforeAfterTester(behavior, calls);
134        t.run();
135    }
136
137    @Test(timeout = 100000)
138    public void testQueueRemoteTargetNoneMember() {
139        Config config = new Config();
140        config.setProperty(GroupProperties.PROP_PARTITION_TABLE_SEND_INTERVAL, "2");
141        final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
142        final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
143        CallBuilder calls = new QueueCallBuilder(h2);
144        final Node node2 = getNode(h2);
145        BeforeAfterBehavior behavior = new BeforeAfterBehavior() {
146            public void before() throws Exception {
147                node2.clusterManager.enqueueAndWait(new Processable() {
148                    public void process() {
149                        node2.clusterManager.removeMember((MemberImpl) h1.getCluster().getLocalMember());
150                        assertEquals(1, node2.clusterManager.lsMembers.size());
151                    }
152                }, 5);
153            }
154
155            public void after() {
156                node2.clusterManager.enqueueAndWait(new Processable() {
157                    public void process() {
158                        MemberImpl secondMember = new MemberImpl(((MemberImpl) h1.getCluster().getLocalMember()).getAddress(), false);
159                        node2.clusterManager.addMember(secondMember);
160                        assertEquals(2, node2.clusterManager.lsMembers.size());
161                    }
162                }, 5);
163            }
164
165            public void destroy() {
166            }
167        };
168        BeforeAfterTester t = new BeforeAfterTester(behavior, calls);
169        t.run();
170    }
171
172    @Ignore
173    class NoneMemberBehavior extends BeforeAfterBehavior {
174        final HazelcastInstance h;
175        final Node node;
176
177        NoneMemberBehavior(HazelcastInstance h) {
178            this.h = h;
179            this.node = getNode(h);
180        }
181
182        @Override
183        void before() throws Exception {
184            List<MemberImpl> lsNew = new LinkedList<MemberImpl>();
185            lsNew.add((MemberImpl) h.getCluster().getLocalMember());
186            lsNew.add(new MemberImpl(new Address("127.0.0.1", 5702), false));
187            node.getClusterImpl().setMembers(lsNew);
188        }
189
190        @Override
191        void after() {
192            List<MemberImpl> lsNew = new LinkedList<MemberImpl>();
193            lsNew.add((MemberImpl) h.getCluster().getLocalMember());
194            node.getClusterImpl().setMembers(lsNew);
195        }
196    }
197}