or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

bookkeeper-client-mocking.mdbookkeeper-server-testing.mdbookkeeper-testing-utilities.mdindex.mdzookeeper-mocking.md

bookkeeper-server-testing.mddocs/

0

# BookKeeper Server Testing

1

2

Server-side testing infrastructure for BookKeeper including server setup, auto-recovery management, and comprehensive testing utilities for integration testing scenarios.

3

4

## Capabilities

5

6

### ServerTester

7

8

Main server testing infrastructure that encapsulates BookKeeper server components with testing controls and auto-recovery management.

9

10

```java { .api }

11

class ServerTester {

12

// Constructors

13

ServerTester(ServerConfiguration conf) throws Exception;

14

ServerTester(ServerConfiguration conf, Bookie b) throws Exception;

15

16

// Auto Recovery Management

17

void startAutoRecovery() throws Exception;

18

void stopAutoRecovery();

19

Auditor getAuditor();

20

ReplicationWorker getReplicationWorker();

21

22

// Access Methods

23

ServerConfiguration getConfiguration();

24

BookieServer getServer();

25

TestStatsProvider getStatsProvider();

26

BookieSocketAddress getAddress();

27

28

// Resource Management

29

void shutdown() throws Exception;

30

}

31

```

32

33

### MockUncleanShutdownDetection

34

35

Mock implementation of unclean shutdown detection for testing server lifecycle and failure scenarios.

36

37

```java { .api }

38

static class MockUncleanShutdownDetection implements UncleanShutdownDetection {

39

// Lifecycle Registration

40

void registerStartUp();

41

void registerCleanShutdown();

42

43

// State Queries

44

boolean lastShutdownWasUnclean();

45

boolean getStartRegistered();

46

boolean getShutdownRegistered();

47

}

48

```

49

50

## Types

51

52

### BookKeeper Server Configuration

53

54

```java { .api }

55

class ServerConfiguration extends AbstractConfiguration {

56

// Common configuration methods for BookKeeper server

57

// (This is part of the BookKeeper API, not defined in testmocks)

58

}

59

60

interface UncleanShutdownDetection {

61

void registerStartUp();

62

void registerCleanShutdown();

63

boolean lastShutdownWasUnclean();

64

}

65

```

66

67

### BookKeeper Server Components

68

69

```java { .api }

70

class BookieServer {

71

// BookKeeper server instance

72

// (Part of BookKeeper server API)

73

}

74

75

class Auditor {

76

// Auto-recovery auditor component

77

// (Part of BookKeeper auto-recovery)

78

}

79

80

class ReplicationWorker {

81

// Auto-recovery replication worker

82

// (Part of BookKeeper auto-recovery)

83

}

84

85

class BookieSocketAddress {

86

// Bookie network address

87

// (Part of BookKeeper API)

88

}

89

```

90

91

## Usage Examples

92

93

### Basic Server Testing

94

95

```java

96

import org.apache.bookkeeper.test.ServerTester;

97

import org.apache.bookkeeper.conf.ServerConfiguration;

98

99

// Create server configuration

100

ServerConfiguration conf = new ServerConfiguration();

101

conf.setBookiePort(0); // Use ephemeral port

102

conf.setJournalDirName("/tmp/bk-journal");

103

conf.setLedgerDirNames(new String[]{"/tmp/bk-ledgers"});

104

conf.setMetadataServiceUri("zk+null://localhost/ledgers");

105

106

// Create and start server tester

107

ServerTester serverTester = new ServerTester(conf);

108

109

// Access server components

110

BookieServer server = serverTester.getServer();

111

BookieSocketAddress address = serverTester.getAddress();

112

TestStatsProvider statsProvider = serverTester.getStatsProvider();

113

114

System.out.println("Server running on: " + address);

115

116

// Shutdown when done

117

serverTester.shutdown();

118

```

119

120

### Auto-Recovery Testing

121

122

```java

123

ServerConfiguration conf = new ServerConfiguration();

124

// Configure as above...

125

126

ServerTester serverTester = new ServerTester(conf);

127

128

// Start auto-recovery components

129

serverTester.startAutoRecovery();

130

131

// Access auto-recovery components

132

Auditor auditor = serverTester.getAuditor();

133

ReplicationWorker replicationWorker = serverTester.getReplicationWorker();

134

135

// Test auto-recovery functionality

136

// (perform operations that trigger recovery)

137

138

// Stop auto-recovery

139

serverTester.stopAutoRecovery();

140

141

// Shutdown server

142

serverTester.shutdown();

143

```

144

145

### Custom Bookie Testing

146

147

```java

148

import org.apache.bookkeeper.bookie.Bookie;

149

150

ServerConfiguration conf = new ServerConfiguration();

151

// Configure as above...

152

153

// Create custom bookie instance

154

Bookie customBookie = new Bookie(conf);

155

156

// Create server tester with custom bookie

157

ServerTester serverTester = new ServerTester(conf, customBookie);

158

159

// Test with custom bookie behavior

160

// ...

161

162

serverTester.shutdown();

163

```

164

165

### Unclean Shutdown Testing

166

167

```java

168

import org.apache.bookkeeper.test.ServerTester.MockUncleanShutdownDetection;

169

170

// Create mock shutdown detection

171

MockUncleanShutdownDetection shutdownDetection = new MockUncleanShutdownDetection();

172

173

// Simulate server startup

174

shutdownDetection.registerStartUp();

175

assert shutdownDetection.getStartRegistered();

176

177

// Simulate clean shutdown

178

shutdownDetection.registerCleanShutdown();

179

assert shutdownDetection.getShutdownRegistered();

180

assert !shutdownDetection.lastShutdownWasUnclean();

181

182

// Test unclean shutdown scenario

183

MockUncleanShutdownDetection uncleanDetection = new MockUncleanShutdownDetection();

184

uncleanDetection.registerStartUp();

185

// Don't register clean shutdown

186

assert uncleanDetection.lastShutdownWasUnclean();

187

```

188

189

### Statistics Provider Testing

190

191

```java

192

ServerTester serverTester = new ServerTester(conf);

193

TestStatsProvider statsProvider = serverTester.getStatsProvider();

194

195

// Access statistics during testing

196

TestStatsProvider.TestOpStatsLogger opStats = statsProvider.getOpStatsLogger("test.operation");

197

TestStatsProvider.TestCounter counter = statsProvider.getCounter("test.counter");

198

199

// Perform operations and verify statistics

200

// ...

201

202

// Check collected statistics

203

long successCount = opStats.getSuccessCount();

204

double avgLatency = opStats.getSuccessAverage();

205

long counterValue = counter.get();

206

207

System.out.println("Operations: " + successCount + ", Avg latency: " + avgLatency);

208

209

serverTester.shutdown();

210

```

211

212

### Integration Testing Pattern

213

214

```java

215

public class BookKeeperIntegrationTest {

216

private ServerTester serverTester;

217

private PulsarMockBookKeeper mockBk;

218

219

@Before

220

public void setUp() throws Exception {

221

// Set up server

222

ServerConfiguration conf = new ServerConfiguration();

223

conf.setBookiePort(0);

224

conf.setJournalDirName(createTempDir());

225

conf.setLedgerDirNames(new String[]{createTempDir()});

226

227

serverTester = new ServerTester(conf);

228

229

// Set up client

230

OrderedExecutor executor = OrderedExecutor.newBuilder()

231

.numThreads(1)

232

.name("test")

233

.build();

234

mockBk = new PulsarMockBookKeeper(executor);

235

}

236

237

@Test

238

public void testServerClientInteraction() throws Exception {

239

// Test server and client together

240

LedgerHandle ledger = mockBk.createLedger(DigestType.CRC32, "password".getBytes());

241

ledger.addEntry("test".getBytes());

242

243

// Verify server statistics

244

TestStatsProvider stats = serverTester.getStatsProvider();

245

// Check relevant metrics...

246

247

ledger.close();

248

}

249

250

@After

251

public void tearDown() throws Exception {

252

if (mockBk != null) {

253

mockBk.close();

254

}

255

if (serverTester != null) {

256

serverTester.shutdown();

257

}

258

}

259

}

260

```