3838import org .lmdbjava .Env .AlreadyClosedException ;
3939import org .lmdbjava .Env .AlreadyOpenException ;
4040import org .lmdbjava .Env .Builder ;
41+ import static org .lmdbjava .Env .Builder .MAX_READERS_DEFAULT ;
4142import org .lmdbjava .Env .InvalidCopyDestination ;
4243import org .lmdbjava .Env .MapFullException ;
4344import static org .lmdbjava .Env .create ;
@@ -59,9 +60,10 @@ public final class EnvTest {
5960 @ Test
6061 public void byteUnit () throws IOException {
6162 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 )) {
6567 final EnvInfo info = env .info ();
6668 assertThat (info .mapSize , is (MEBIBYTES .toBytes (1 )));
6769 }
@@ -70,31 +72,38 @@ public void byteUnit() throws IOException {
7072 @ Test (expected = AlreadyOpenException .class )
7173 public void cannotChangeMapSizeAfterOpen () throws IOException {
7274 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+ }
7680 }
7781
7882 @ Test (expected = AlreadyOpenException .class )
7983 public void cannotChangeMaxDbsAfterOpen () throws IOException {
8084 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+ }
8490 }
8591
8692 @ Test (expected = AlreadyOpenException .class )
8793 public void cannotChangeMaxReadersAfterOpen () throws IOException {
8894 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+ }
92100 }
93101
94102 @ Test (expected = AlreadyClosedException .class )
95103 public void cannotInfoOnceClosed () throws IOException {
96104 final File path = tmp .newFile ();
97105 final Env <ByteBuffer > env = create ()
106+ .setMaxReaders (1 )
98107 .open (path , MDB_NOSUBDIR );
99108 env .close ();
100109 env .info ();
@@ -103,14 +112,17 @@ public void cannotInfoOnceClosed() throws IOException {
103112 @ Test (expected = AlreadyOpenException .class )
104113 public void cannotOpenTwice () throws IOException {
105114 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 ();
108119 builder .open (path , MDB_NOSUBDIR );
109120 }
110121
111122 @ Test (expected = IllegalArgumentException .class )
112123 public void cannotOverflowMapSize () {
113- final Builder <ByteBuffer > builder = create ();
124+ final Builder <ByteBuffer > builder = create ()
125+ .setMaxReaders (1 );
114126 final int mb = 1_024 * 1_024 ;
115127 final int size = mb * 2_048 ; // as per issue 18
116128 builder .setMapSize (size );
@@ -120,6 +132,7 @@ public void cannotOverflowMapSize() {
120132 public void cannotStatOnceClosed () throws IOException {
121133 final File path = tmp .newFile ();
122134 final Env <ByteBuffer > env = create ()
135+ .setMaxReaders (1 )
123136 .open (path , MDB_NOSUBDIR );
124137 env .close ();
125138 env .stat ();
@@ -129,6 +142,7 @@ public void cannotStatOnceClosed() throws IOException {
129142 public void cannotSyncOnceClosed () throws IOException {
130143 final File path = tmp .newFile ();
131144 final Env <ByteBuffer > env = create ()
145+ .setMaxReaders (1 )
132146 .open (path , MDB_NOSUBDIR );
133147 env .close ();
134148 env .sync (false );
@@ -141,7 +155,9 @@ public void copy() throws IOException {
141155 assertThat (dest .isDirectory (), is (true ));
142156 assertThat (dest .list ().length , is (0 ));
143157 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 )) {
145161 env .copy (dest , MDB_CP_COMPACT );
146162 assertThat (dest .list ().length , is (1 ));
147163 }
@@ -151,7 +167,9 @@ public void copy() throws IOException {
151167 public void copyRejectsFileDestination () throws IOException {
152168 final File dest = tmp .newFile ();
153169 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 )) {
155173 env .copy (dest , MDB_CP_COMPACT );
156174 }
157175 }
@@ -161,7 +179,9 @@ public void copyRejectsMissingDestination() throws IOException {
161179 final File dest = tmp .newFolder ();
162180 assertThat (dest .delete (), is (true ));
163181 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 )) {
165185 env .copy (dest , MDB_CP_COMPACT );
166186 }
167187 }
@@ -172,15 +192,19 @@ public void copyRejectsNonEmptyDestination() throws IOException {
172192 final File subDir = new File (dest , "hello" );
173193 assertThat (subDir .mkdir (), is (true ));
174194 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 )) {
176198 env .copy (dest , MDB_CP_COMPACT );
177199 }
178200 }
179201
180202 @ Test
181203 public void createAsDirectory () throws IOException {
182204 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 );
184208 assertThat (path .isDirectory (), is (true ));
185209 env .sync (false );
186210 env .close ();
@@ -204,10 +228,12 @@ public void createAsFile() throws IOException {
204228 @ Test (expected = BadReaderLockException .class )
205229 public void detectTransactionThreadViolation () throws IOException {
206230 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+ }
211237 }
212238
213239 @ Test
@@ -237,7 +263,9 @@ public void mapFull() throws IOException {
237263 final ByteBuffer key = allocateDirect (500 );
238264 final ByteBuffer val = allocateDirect (1_024 );
239265 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 ))
241269 .setMaxDbs (1 ).open (path )) {
242270 final Dbi <ByteBuffer > db = env .openDbi (DB_1 , MDB_CREATE );
243271 for (;;) {
@@ -253,11 +281,15 @@ public void mapFull() throws IOException {
253281 @ Test
254282 public void readOnlySupported () throws IOException {
255283 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 )) {
257287 final Dbi <ByteBuffer > rwDb = rwEnv .openDbi (DB_1 , MDB_CREATE );
258288 rwDb .put (bb (1 ), bb (42 ));
259289 }
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 )) {
261293 final Dbi <ByteBuffer > roDb = roEnv .openDbi (DB_1 );
262294 try (Txn <ByteBuffer > roTxn = roEnv .txnRead ()) {
263295 assertThat (roDb .get (roTxn , bb (1 )), notNullValue ());
@@ -272,8 +304,11 @@ public void setMapSize() throws IOException {
272304 final ByteBuffer key = allocateDirect (500 );
273305 final ByteBuffer val = allocateDirect (1_024 );
274306 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 )) {
277312 final Dbi <ByteBuffer > db = env .openDbi (DB_1 , MDB_CREATE );
278313
279314 db .put (bb (1 ), bb (42 ));
@@ -316,7 +351,9 @@ public void setMapSize() throws IOException {
316351 @ Test
317352 public void stats () throws IOException {
318353 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 )) {
320357 final Stat stat = env .stat ();
321358 assertThat (stat , is (notNullValue ()));
322359 assertThat (stat .branchPages , is (0L ));
0 commit comments