Skip to content

Commit 026d16b

Browse files
Merge pull request #200 from neilcsmith-net/issue-176
Issue 176 - Promise change listener being garbage collected
2 parents 83f7910 + cce4cf5 commit 026d16b

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

src/org/freedesktop/gstreamer/Promise.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Neil C Smith
2+
* Copyright (c) 2020 Neil C Smith
33
* Copyright (c) 2018 Vinicius Tona
44
* Copyright (c) 2018 Antonio Morales
55
*
@@ -38,6 +38,8 @@ public class Promise extends MiniObject {
3838

3939
public static final String GTYPE_NAME = "GstPromise";
4040

41+
private GstCallback changeFunction;
42+
4143
/**
4244
* Creates a new instance of Promise. This constructor is used internally.
4345
*
@@ -62,12 +64,17 @@ public Promise() {
6264
* {@link Promise} is changed
6365
*/
6466
public Promise(final PROMISE_CHANGE listener) {
65-
this(Natives.initializer(GSTPROMISE_API.ptr_gst_promise_new_with_change_func(new GstCallback() {
66-
@SuppressWarnings("unused")
67+
this(new GstCallback() {
6768
public void callback(Promise promise, Pointer userData) {
6869
listener.onChange(promise);
6970
}
70-
}, null, null)));
71+
});
72+
}
73+
74+
private Promise(GstCallback callback) {
75+
this(Natives.initializer(GSTPROMISE_API
76+
.ptr_gst_promise_new_with_change_func(callback, null, null)));
77+
this.changeFunction = callback;
7178
}
7279

7380
/**

test/org/freedesktop/gstreamer/PromiseTest.java

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
* Copyright (c) 2020 Neil C Smith
3+
* Copyright (c) 2019 Kezhu Wang
24
* Copyright (c) 2018 Antonio Morales
35
*
46
* This file is part of gstreamer-java.
@@ -18,16 +20,18 @@
1820
*/
1921
package org.freedesktop.gstreamer;
2022

23+
import java.util.concurrent.atomic.AtomicBoolean;
24+
2125
import static org.junit.Assert.assertEquals;
2226
import static org.junit.Assert.assertFalse;
2327
import static org.junit.Assert.assertTrue;
2428

2529
import org.freedesktop.gstreamer.glib.Natives;
2630
import org.freedesktop.gstreamer.lowlevel.GPointer;
2731
import org.freedesktop.gstreamer.lowlevel.GType;
32+
import org.freedesktop.gstreamer.util.TestAssumptions;
2833
import org.junit.BeforeClass;
2934
import org.junit.AfterClass;
30-
import org.junit.Before;
3135
import org.junit.Test;
3236

3337
public class PromiseTest {
@@ -37,7 +41,7 @@ public PromiseTest() {
3741

3842
@BeforeClass
3943
public static void setUpClass() throws Exception {
40-
Gst.init(Gst.getVersion(), "PromiseTest", new String[] {});
44+
Gst.init(Gst.getVersion(), "PromiseTest");
4145
}
4246

4347
@AfterClass
@@ -47,9 +51,8 @@ public static void tearDownClass() throws Exception {
4751

4852
@Test
4953
public void testReply() {
50-
if (!Gst.testVersion(1, 14)) {
51-
return;
52-
}
54+
TestAssumptions.requireGstVersion(1, 14);
55+
5356
Promise promise = new Promise();
5457

5558
promise.reply(null);
@@ -61,9 +64,8 @@ public void testReply() {
6164

6265
@Test
6366
public void testInterrupt() {
64-
if (!Gst.testVersion(1, 14)) {
65-
return;
66-
}
67+
TestAssumptions.requireGstVersion(1, 14);
68+
6769
Promise promise = new Promise();
6870
promise.interrupt();
6971

@@ -74,9 +76,8 @@ public void testInterrupt() {
7476

7577
@Test
7678
public void testExpire() {
77-
if (!Gst.testVersion(1, 14)) {
78-
return;
79-
}
79+
TestAssumptions.requireGstVersion(1, 14);
80+
8081
Promise promise = new Promise();
8182
promise.expire();
8283

@@ -87,9 +88,8 @@ public void testExpire() {
8788

8889
@Test
8990
public void testInvalidateReply() {
90-
if (!Gst.testVersion(1, 14)) {
91-
return;
92-
}
91+
TestAssumptions.requireGstVersion(1, 14);
92+
9393
Promise promise = new Promise();
9494
Structure data = new Structure("data");
9595

@@ -101,9 +101,8 @@ public void testInvalidateReply() {
101101

102102
@Test
103103
public void testReplyData() {
104-
if (!Gst.testVersion(1, 14)) {
105-
return;
106-
}
104+
TestAssumptions.requireGstVersion(1, 14);
105+
107106
Promise promise = new Promise();
108107
Structure data = new Structure("data", "test", GType.UINT, 1);
109108
GPointer pointer = Natives.getPointer(data);
@@ -117,25 +116,42 @@ public void testReplyData() {
117116

118117
@Test
119118
public void testDispose() {
120-
if (!Gst.testVersion(1, 14)) {
121-
return;
122-
}
119+
TestAssumptions.requireGstVersion(1, 14);
120+
123121
Promise promise = new Promise();
124122
promise.interrupt();
125123
promise.dispose();
126124
}
127125

128126
@Test
129127
public void testDisposeWithChangeFunc() {
130-
if (!Gst.testVersion(1, 14)) {
131-
return;
132-
}
128+
TestAssumptions.requireGstVersion(1, 14);
129+
130+
Promise promise = new Promise(new Promise.PROMISE_CHANGE() {
131+
@Override
132+
public void onChange(Promise promise) {
133+
}
134+
});
135+
promise.interrupt();
136+
promise.dispose();
137+
}
138+
139+
@Test
140+
public void testChangeFunctionGC() {
141+
TestAssumptions.requireGstVersion(1, 14);
142+
143+
final AtomicBoolean onChangeFired = new AtomicBoolean(false);
144+
133145
Promise promise = new Promise(new Promise.PROMISE_CHANGE() {
134146
@Override
135147
public void onChange(Promise promise) {
148+
onChangeFired.set(true);
136149
}
137150
});
151+
System.gc();
152+
System.gc();
138153
promise.interrupt();
154+
assertTrue("Promise Change callback GC'd", onChangeFired.get());
139155
promise.dispose();
140156
}
141157
}

0 commit comments

Comments
 (0)