55
66import static org .junit .jupiter .api .Assertions .assertNotNull ;
77import static org .junit .jupiter .api .Assertions .assertTrue ;
8+ import org .junit .jupiter .api .Disabled ;
89import org .junit .jupiter .api .Test ;
910
1011import com .surrealdb .pojos .Person ;
@@ -19,27 +20,28 @@ public class LiveQueryTests {
1920 void selectLiveReturnsLiveStream () {
2021 try (Surreal surreal = new Surreal ()) {
2122 surreal .connect ("memory" ).useNs ("test" ).useDb ("test" );
22- LiveStream stream = surreal .selectLive ("person" );
23- assertNotNull (stream );
24- stream . close ();
23+ try ( LiveStream stream = surreal .selectLive ("person" )) {
24+ assertNotNull (stream );
25+ }
2526 }
2627 }
2728
2829 @ Test
2930 void liveStreamNextCanBlockAndClose () throws Exception {
31+ AtomicReference <Optional <LiveNotification >> result = new AtomicReference <>();
3032 try (Surreal surreal = new Surreal ()) {
3133 surreal .connect ("memory" ).useNs ("test" ).useDb ("test" );
3234 surreal .create (Person .class , "person" , Helpers .tobie );
33- LiveStream stream = surreal . selectLive ( "person" ) ;
34- assertNotNull ( stream );
35- // next() blocks until a notification or close; run in background and close
36- // after short timeout
37- AtomicReference < Optional < LiveNotification >> result = new AtomicReference <>();
38- Thread consumer = new Thread (() -> result .set (stream .next ()));
39- consumer .setDaemon (true );
40- consumer .start ();
41- Thread .sleep (500 );
42- stream . close ();
35+ Thread consumer ;
36+ try ( LiveStream stream = surreal . selectLive ( "person" )) {
37+ assertNotNull ( stream );
38+ // next() blocks until a notification or close; run in background and close
39+ // after short timeout
40+ consumer = new Thread (() -> result .set (stream .next ()));
41+ consumer .setDaemon (true );
42+ consumer .start ();
43+ Thread .sleep (500 );
44+ }
4345 consumer .join (2000 );
4446 assertNotNull (result .get ());
4547 }
@@ -49,11 +51,35 @@ void liveStreamNextCanBlockAndClose() throws Exception {
4951 void liveStreamCloseReleases () {
5052 try (Surreal surreal = new Surreal ()) {
5153 surreal .connect ("memory" ).useNs ("test" ).useDb ("test" );
52- LiveStream stream = surreal .selectLive ("person" );
53- stream . close ();
54- // No exception; closing again or using after close is undefined but we don't
55- // crash
54+ try ( LiveStream stream = surreal .selectLive ("person" )) {
55+ // No exception; closing again or using after close is undefined but we don't
56+ // crash
57+ }
5658 assertTrue (true );
5759 }
5860 }
61+
62+ @ Test
63+ void liveStreamTryWithResources () {
64+ try (Surreal surreal = new Surreal ()) {
65+ surreal .connect ("memory" ).useNs ("test" ).useDb ("test" );
66+ try (LiveStream stream = surreal .selectLive ("person" )) {
67+ assertNotNull (stream );
68+ }
69+ // stream closed automatically; no leak
70+ }
71+ }
72+
73+ /**
74+ * Placeholder for future Surreal.kill(liveQueryId) support. The query ID is
75+ * available from {@link LiveNotification#getQueryId()}, but the Java client
76+ * does not yet expose kill(). Use {@link LiveStream#close()} to stop a live
77+ * query.
78+ */
79+ @ Test
80+ @ Disabled ("Surreal.kill(liveQueryId) not yet in Java API" )
81+ void killLiveQuery_byQueryId () {
82+ // When kill(uuid) is added: start live query, get queryId from first
83+ // notification or API, call surreal.kill(queryId), assert stream ends.
84+ }
5985}
0 commit comments