1+ /*
2+ * Copyright 2024 ObjectBox Ltd. 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+
17+ package io .objectbox ;
18+
19+ import org .junit .Test ;
20+
21+ import java .io .ByteArrayOutputStream ;
22+ import java .io .PrintStream ;
23+ import java .io .UnsupportedEncodingException ;
24+
25+ import javax .annotation .Nullable ;
26+
27+ import io .objectbox .annotation .IndexType ;
28+
29+
30+ import static org .junit .Assert .assertTrue ;
31+
32+ /**
33+ * Sets a custom error output stream to assert log messages of {@link BoxStore}.
34+ */
35+ public class BoxStoreLogTest extends AbstractObjectBoxTest {
36+
37+ private ByteArrayOutputStream errOutput ;
38+
39+ @ Override
40+ protected BoxStoreBuilder createBoxStoreBuilder (@ Nullable IndexType simpleStringIndexType ) {
41+ BoxStoreBuilder builder = super .createBoxStoreBuilder (simpleStringIndexType );
42+ errOutput = new ByteArrayOutputStream ();
43+ builder .setErrorOutput (new PrintStream (errOutput ));
44+ return builder ;
45+ }
46+
47+ @ Test
48+ public void close_activeThreadPool_printsError () throws UnsupportedEncodingException {
49+ // Submit two threads, one to the internal pool, that run longer
50+ // than BoxStore.close waits on the pool to terminate
51+ new Thread (() -> {
52+ try {
53+ Thread .sleep (2000 );
54+ } catch (InterruptedException ignored ) {
55+ }
56+ }).start ();
57+ store .internalScheduleThread (() -> {
58+ try {
59+ Thread .sleep (2000 );
60+ } catch (InterruptedException ignored ) {
61+ }
62+ });
63+ // Close store to trigger thread pool shutdown, store waits 1 second for shutdown
64+ store .close ();
65+
66+ String errOutput = this .errOutput .toString ("UTF-8" );
67+ assertTrue (errOutput .contains ("ObjectBox thread pool not terminated in time" ));
68+ assertTrue (errOutput .contains ("Printing pool threads..." ));
69+ assertTrue (errOutput .contains ("Printing pool threads...DONE" ));
70+ // Check that only ObjectBox threads are printed
71+ String [] lines = errOutput .split ("\n " );
72+ for (String line : lines ) {
73+ if (line .startsWith ("Thread:" )) {
74+ assertTrue ("Expected thread name to start with 'ObjectBox-' but was: " + line , line .contains ("ObjectBox-" ));
75+ }
76+ }
77+ }
78+
79+ }
0 commit comments