1
2/*
3 * A test case that brings up the replication
4 * manager infrastructure as master. Then shut
5 * the master down cleanly.
6 * This case does not have any replication clients
7 * or even update the underlying DB.
8 */
9
10package com.sleepycat.db.test;
11
12import com.sleepycat.db.test.TestUtils;
13
14import org.junit.Before;
15import org.junit.BeforeClass;
16import org.junit.After;
17import org.junit.AfterClass;
18import org.junit.Test;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.fail;
21import junit.framework.JUnit4TestAdapter;
22
23import java.io.File;
24import java.io.FileNotFoundException;
25
26import com.sleepycat.db.*;
27
28public class RepmgrStartupTest extends EventHandlerAdapter
29{
30    static String address = "localhost";
31    static int    port = 4242;
32    static int    priority = 100;
33    static String homedirName = "TESTDIR";
34    File homedir;
35    EnvironmentConfig envConfig;
36    Environment dbenv;
37
38    @BeforeClass public static void ClassInit() {
39	    TestUtils.loadConfig(null);
40    }
41
42    @AfterClass public static void ClassShutdown() {
43    }
44
45    @Before public void PerTestInit()
46    {
47        TestUtils.removeDir(homedirName);
48        try {
49            homedir = new File(homedirName);
50            homedir.mkdir();
51        } catch (Exception e) {
52            TestUtils.DEBUGOUT(2, "Warning: initialization had a problem creating a clean directory.\n" + e);
53        }
54        try {
55            homedir = new File(homedirName);
56        } catch (NullPointerException npe) {
57            // can't really happen :)
58        }
59        envConfig = new EnvironmentConfig();
60        envConfig.setErrorStream(TestUtils.getErrorStream());
61        envConfig.setErrorPrefix("RepmgrStartupTest test");
62        envConfig.setAllowCreate(true);
63        envConfig.setRunRecovery(true);
64        envConfig.setThreaded(true);
65        envConfig.setInitializeLocking(true);
66        envConfig.setInitializeLogging(true);
67        envConfig.setInitializeCache(true);
68        envConfig.setTransactional(true);
69        envConfig.setTxnNoSync(true);
70        envConfig.setInitializeReplication(true);
71        envConfig.setVerboseReplication(false);
72
73        ReplicationHostAddress haddr = new ReplicationHostAddress(address, port);
74        envConfig.setReplicationManagerLocalSite(haddr);
75        envConfig.setReplicationPriority(priority);
76        envConfig.setEventHandler(this);
77        envConfig.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);
78
79        try {
80            dbenv = new Environment(homedir, envConfig);
81        } catch(FileNotFoundException e) {
82            fail("Unexpected FNFE in standard environment creation." + e);
83        } catch(DatabaseException dbe) {
84            fail("Unexpected database exception came from environment create." + dbe);
85        }
86    }
87
88    @After public void PerTestShutdown()
89        throws Exception {
90	    try {
91            File homedir = new File(homedirName);
92
93            if (homedir.exists()) {
94                // The following will fail if the directory contains sub-dirs.
95                if (homedir.isDirectory()) {
96                    File[] contents = homedir.listFiles();
97                    for (int i = 0; i < contents.length; i++)
98                        contents[i].delete();
99                }
100                homedir.delete();
101            }
102        } catch (Exception e) {
103            TestUtils.DEBUGOUT(2, "Warning: shutdown had a problem cleaning up test directory.\n" + e);
104        }
105    }
106
107
108    @Test (timeout=3000) public void startMaster()
109    {
110        try {
111            // start replication manager
112            dbenv.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_MASTER);
113        } catch(DatabaseException dbe) {
114            fail("Unexpected database exception came from replicationManagerStart." + dbe);
115        }
116        try {
117            java.lang.Thread.sleep(1000);
118        }catch(InterruptedException ie) {}
119
120        try {
121            dbenv.close();
122            Environment.remove(homedir, false, envConfig);
123        } catch(FileNotFoundException fnfe) {
124        } catch(DatabaseException dbe) {
125            fail("Unexpected database exception came during shutdown." + dbe);
126        }
127    }
128
129    @Test (timeout=3000) public void startClient()
130    {
131        try {
132            // start replication manager
133            dbenv.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_CLIENT);
134        } catch(DatabaseException dbe) {
135            fail("Unexpected database exception came from replicationManagerStart." + dbe);
136        }
137        try {
138            java.lang.Thread.sleep(1000);
139        }catch(InterruptedException ie) {}
140
141        try {
142            dbenv.close();
143            Environment.remove(homedir, false, envConfig);
144        } catch(FileNotFoundException fnfe) {
145        } catch(DatabaseException dbe) {
146            fail("Unexpected database exception came during shutdown." + dbe);
147        }
148    }
149
150    @Test (timeout=3000) public void startElection()
151    {
152        try {
153            // start replication manager
154            dbenv.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_ELECTION);
155        } catch(DatabaseException dbe) {
156            fail("Unexpected database exception came from replicationManagerStart." + dbe);
157        }
158        try {
159            java.lang.Thread.sleep(1000);
160        }catch(InterruptedException ie) {}
161
162        try {
163            dbenv.close();
164            Environment.remove(homedir, false, envConfig);
165        } catch(FileNotFoundException fnfe) {
166        } catch(DatabaseException dbe) {
167            fail("Unexpected database exception came during shutdown." + dbe);
168        }
169    }
170
171    @Test (timeout=15000) public void startMasterWaitBeforeShutdown()
172    {
173        try {
174            // start replication manager
175            dbenv.replicationManagerStart(3, ReplicationManagerStartPolicy.REP_MASTER);
176        } catch(DatabaseException dbe) {
177            fail("Unexpected database exception came from replicationManagerStart." + dbe.toString());
178        }
179        try {
180            /*
181             * NOTE! This is a bit alarming - I have seen shutdown failures with the following message:
182             *
183             * RepmgrStartupTest test: Waiting for handle count (1) or msg_th (0) to complete replication lockout
184             *
185             * When the sleep is over 10 seconds.
186             */
187            java.lang.Thread.sleep(12000);
188        }catch(InterruptedException ie) {}
189
190        try {
191            dbenv.close();
192            Environment.remove(homedir, false, envConfig);
193        } catch(FileNotFoundException fnfe) {
194        } catch(DatabaseException dbe) {
195            fail("Unexpected database exception came during shutdown." + dbe.toString());
196        }
197    }
198
199    public void handleRepMasterEvent() {
200        TestUtils.DEBUGOUT(1, "Got a REP_MASTER message");
201    }
202
203    public void handleRepClientEvent() {
204        TestUtils.DEBUGOUT(1, "Got a REP_CLIENT message");
205    }
206
207    public void handleRepNewMasterEvent() {
208        TestUtils.DEBUGOUT(1, "Got a REP_NEW_MASTER message");
209    }
210}
211