38
38
import org .lmdbjava .Env .AlreadyClosedException ;
39
39
import org .lmdbjava .Env .AlreadyOpenException ;
40
40
import org .lmdbjava .Env .Builder ;
41
+ import static org .lmdbjava .Env .Builder .MAX_READERS_DEFAULT ;
41
42
import org .lmdbjava .Env .InvalidCopyDestination ;
42
43
import org .lmdbjava .Env .MapFullException ;
43
44
import static org .lmdbjava .Env .create ;
@@ -59,9 +60,10 @@ public final class EnvTest {
59
60
@ Test
60
61
public void byteUnit () throws IOException {
61
62
final File path = tmp .newFile ();
62
- try (Env <ByteBuffer > env
63
- = create ().setMapSize (MEBIBYTES .toBytes (1 )).open (
64
- path , MDB_NOSUBDIR )) {
63
+ try (Env <ByteBuffer > env = create ()
64
+ .setMaxReaders (1 )
65
+ .setMapSize (MEBIBYTES .toBytes (1 ))
66
+ .open (path , MDB_NOSUBDIR )) {
65
67
final EnvInfo info = env .info ();
66
68
assertThat (info .mapSize , is (MEBIBYTES .toBytes (1 )));
67
69
}
@@ -70,31 +72,38 @@ public void byteUnit() throws IOException {
70
72
@ Test (expected = AlreadyOpenException .class )
71
73
public void cannotChangeMapSizeAfterOpen () throws IOException {
72
74
final File path = tmp .newFile ();
73
- final Builder <ByteBuffer > builder = create ();
74
- builder .open (path , MDB_NOSUBDIR );
75
- builder .setMapSize (1 );
75
+ final Builder <ByteBuffer > builder = create ()
76
+ .setMaxReaders (1 );
77
+ try (Env <ByteBuffer > env = builder .open (path , MDB_NOSUBDIR )) {
78
+ builder .setMapSize (1 );
79
+ }
76
80
}
77
81
78
82
@ Test (expected = AlreadyOpenException .class )
79
83
public void cannotChangeMaxDbsAfterOpen () throws IOException {
80
84
final File path = tmp .newFile ();
81
- final Builder <ByteBuffer > builder = create ();
82
- builder .open (path , MDB_NOSUBDIR );
83
- builder .setMaxDbs (1 );
85
+ final Builder <ByteBuffer > builder = create ()
86
+ .setMaxReaders (1 );
87
+ try (Env <ByteBuffer > env = builder .open (path , MDB_NOSUBDIR )) {
88
+ builder .setMaxDbs (1 );
89
+ }
84
90
}
85
91
86
92
@ Test (expected = AlreadyOpenException .class )
87
93
public void cannotChangeMaxReadersAfterOpen () throws IOException {
88
94
final File path = tmp .newFile ();
89
- final Builder <ByteBuffer > builder = create ();
90
- builder .open (path , MDB_NOSUBDIR );
91
- builder .setMaxReaders (1 );
95
+ final Builder <ByteBuffer > builder = create ()
96
+ .setMaxReaders (1 );
97
+ try (Env <ByteBuffer > env = builder .open (path , MDB_NOSUBDIR )) {
98
+ builder .setMaxReaders (1 );
99
+ }
92
100
}
93
101
94
102
@ Test (expected = AlreadyClosedException .class )
95
103
public void cannotInfoOnceClosed () throws IOException {
96
104
final File path = tmp .newFile ();
97
105
final Env <ByteBuffer > env = create ()
106
+ .setMaxReaders (1 )
98
107
.open (path , MDB_NOSUBDIR );
99
108
env .close ();
100
109
env .info ();
@@ -103,14 +112,17 @@ public void cannotInfoOnceClosed() throws IOException {
103
112
@ Test (expected = AlreadyOpenException .class )
104
113
public void cannotOpenTwice () throws IOException {
105
114
final File path = tmp .newFile ();
106
- final Builder <ByteBuffer > builder = create ();
107
- builder .open (path , MDB_NOSUBDIR );
115
+ final Builder <ByteBuffer > builder = create ()
116
+ .setMaxReaders (1 );
117
+
118
+ builder .open (path , MDB_NOSUBDIR ).close ();
108
119
builder .open (path , MDB_NOSUBDIR );
109
120
}
110
121
111
122
@ Test (expected = IllegalArgumentException .class )
112
123
public void cannotOverflowMapSize () {
113
- final Builder <ByteBuffer > builder = create ();
124
+ final Builder <ByteBuffer > builder = create ()
125
+ .setMaxReaders (1 );
114
126
final int mb = 1_024 * 1_024 ;
115
127
final int size = mb * 2_048 ; // as per issue 18
116
128
builder .setMapSize (size );
@@ -120,6 +132,7 @@ public void cannotOverflowMapSize() {
120
132
public void cannotStatOnceClosed () throws IOException {
121
133
final File path = tmp .newFile ();
122
134
final Env <ByteBuffer > env = create ()
135
+ .setMaxReaders (1 )
123
136
.open (path , MDB_NOSUBDIR );
124
137
env .close ();
125
138
env .stat ();
@@ -129,6 +142,7 @@ public void cannotStatOnceClosed() throws IOException {
129
142
public void cannotSyncOnceClosed () throws IOException {
130
143
final File path = tmp .newFile ();
131
144
final Env <ByteBuffer > env = create ()
145
+ .setMaxReaders (1 )
132
146
.open (path , MDB_NOSUBDIR );
133
147
env .close ();
134
148
env .sync (false );
@@ -141,7 +155,9 @@ public void copy() throws IOException {
141
155
assertThat (dest .isDirectory (), is (true ));
142
156
assertThat (dest .list ().length , is (0 ));
143
157
final File src = tmp .newFolder ();
144
- try (Env <ByteBuffer > env = create ().open (src )) {
158
+ try (Env <ByteBuffer > env = create ()
159
+ .setMaxReaders (1 )
160
+ .open (src )) {
145
161
env .copy (dest , MDB_CP_COMPACT );
146
162
assertThat (dest .list ().length , is (1 ));
147
163
}
@@ -151,7 +167,9 @@ public void copy() throws IOException {
151
167
public void copyRejectsFileDestination () throws IOException {
152
168
final File dest = tmp .newFile ();
153
169
final File src = tmp .newFolder ();
154
- try (Env <ByteBuffer > env = create ().open (src )) {
170
+ try (Env <ByteBuffer > env = create ()
171
+ .setMaxReaders (1 )
172
+ .open (src )) {
155
173
env .copy (dest , MDB_CP_COMPACT );
156
174
}
157
175
}
@@ -161,7 +179,9 @@ public void copyRejectsMissingDestination() throws IOException {
161
179
final File dest = tmp .newFolder ();
162
180
assertThat (dest .delete (), is (true ));
163
181
final File src = tmp .newFolder ();
164
- try (Env <ByteBuffer > env = create ().open (src )) {
182
+ try (Env <ByteBuffer > env = create ()
183
+ .setMaxReaders (1 )
184
+ .open (src )) {
165
185
env .copy (dest , MDB_CP_COMPACT );
166
186
}
167
187
}
@@ -172,15 +192,19 @@ public void copyRejectsNonEmptyDestination() throws IOException {
172
192
final File subDir = new File (dest , "hello" );
173
193
assertThat (subDir .mkdir (), is (true ));
174
194
final File src = tmp .newFolder ();
175
- try (Env <ByteBuffer > env = create ().open (src )) {
195
+ try (Env <ByteBuffer > env = create ()
196
+ .setMaxReaders (1 )
197
+ .open (src )) {
176
198
env .copy (dest , MDB_CP_COMPACT );
177
199
}
178
200
}
179
201
180
202
@ Test
181
203
public void createAsDirectory () throws IOException {
182
204
final File path = tmp .newFolder ();
183
- final Env <ByteBuffer > env = create ().open (path );
205
+ final Env <ByteBuffer > env = create ()
206
+ .setMaxReaders (1 )
207
+ .open (path );
184
208
assertThat (path .isDirectory (), is (true ));
185
209
env .sync (false );
186
210
env .close ();
@@ -204,10 +228,12 @@ public void createAsFile() throws IOException {
204
228
@ Test (expected = BadReaderLockException .class )
205
229
public void detectTransactionThreadViolation () throws IOException {
206
230
final File path = tmp .newFile ();
207
- final Env <ByteBuffer > env = create ()
208
- .open (path , MDB_NOSUBDIR );
209
- env .txnRead ();
210
- env .txnRead ();
231
+ try (Env <ByteBuffer > env = create ()
232
+ .setMaxReaders (1 )
233
+ .open (path , MDB_NOSUBDIR )) {
234
+ env .txnRead ();
235
+ env .txnRead ();
236
+ }
211
237
}
212
238
213
239
@ Test
@@ -237,7 +263,9 @@ public void mapFull() throws IOException {
237
263
final ByteBuffer key = allocateDirect (500 );
238
264
final ByteBuffer val = allocateDirect (1_024 );
239
265
final Random rnd = new Random ();
240
- try (Env <ByteBuffer > env = create ().setMapSize (MEBIBYTES .toBytes (8 ))
266
+ try (Env <ByteBuffer > env = create ()
267
+ .setMaxReaders (1 )
268
+ .setMapSize (MEBIBYTES .toBytes (8 ))
241
269
.setMaxDbs (1 ).open (path )) {
242
270
final Dbi <ByteBuffer > db = env .openDbi (DB_1 , MDB_CREATE );
243
271
for (;;) {
@@ -253,11 +281,15 @@ public void mapFull() throws IOException {
253
281
@ Test
254
282
public void readOnlySupported () throws IOException {
255
283
final File path = tmp .newFolder ();
256
- try (Env <ByteBuffer > rwEnv = create ().open (path )) {
284
+ try (Env <ByteBuffer > rwEnv = create ()
285
+ .setMaxReaders (1 )
286
+ .open (path )) {
257
287
final Dbi <ByteBuffer > rwDb = rwEnv .openDbi (DB_1 , MDB_CREATE );
258
288
rwDb .put (bb (1 ), bb (42 ));
259
289
}
260
- try (Env <ByteBuffer > roEnv = create ().open (path , MDB_RDONLY_ENV )) {
290
+ try (Env <ByteBuffer > roEnv = create ()
291
+ .setMaxReaders (1 )
292
+ .open (path , MDB_RDONLY_ENV )) {
261
293
final Dbi <ByteBuffer > roDb = roEnv .openDbi (DB_1 );
262
294
try (Txn <ByteBuffer > roTxn = roEnv .txnRead ()) {
263
295
assertThat (roDb .get (roTxn , bb (1 )), notNullValue ());
@@ -272,8 +304,11 @@ public void setMapSize() throws IOException {
272
304
final ByteBuffer key = allocateDirect (500 );
273
305
final ByteBuffer val = allocateDirect (1_024 );
274
306
final Random rnd = new Random ();
275
- try (Env <ByteBuffer > env = create ().setMapSize (50_000 )
276
- .setMaxDbs (1 ).open (path )) {
307
+ try (Env <ByteBuffer > env = create ()
308
+ .setMaxReaders (1 )
309
+ .setMapSize (50_000 )
310
+ .setMaxDbs (1 )
311
+ .open (path )) {
277
312
final Dbi <ByteBuffer > db = env .openDbi (DB_1 , MDB_CREATE );
278
313
279
314
db .put (bb (1 ), bb (42 ));
@@ -316,7 +351,9 @@ public void setMapSize() throws IOException {
316
351
@ Test
317
352
public void stats () throws IOException {
318
353
final File path = tmp .newFile ();
319
- try (Env <ByteBuffer > env = create ().open (path , MDB_NOSUBDIR )) {
354
+ try (Env <ByteBuffer > env = create ()
355
+ .setMaxReaders (1 )
356
+ .open (path , MDB_NOSUBDIR )) {
320
357
final Stat stat = env .stat ();
321
358
assertThat (stat , is (notNullValue ()));
322
359
assertThat (stat .branchPages , is (0L ));
@@ -333,6 +370,8 @@ public void stats() throws IOException {
333
370
public void testDefaultOpen () throws IOException {
334
371
final File path = tmp .newFolder ();
335
372
try (Env <ByteBuffer > env = open (path , 10 )) {
373
+ final EnvInfo info = env .info ();
374
+ assertThat (info .maxReaders , is (MAX_READERS_DEFAULT ));
336
375
final Dbi <ByteBuffer > db = env .openDbi ("test" , MDB_CREATE );
337
376
db .put (allocateDirect (1 ), allocateDirect (1 ));
338
377
}
0 commit comments