Skip to content

Commit

Permalink
fix: thread hangs after running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Sep 28, 2021
1 parent 585c16a commit 9c3db9a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public class GlCalculatorHelperTest {
#region Constructor
[Test, GpuOnly]
public void Ctor_ShouldInstantiateGlCalculatorHelper() {
var glCalculatorHelper = new GlCalculatorHelper();

Assert.AreNotEqual(glCalculatorHelper.mpPtr, IntPtr.Zero);
using (var glCalculatorHelper = new GlCalculatorHelper()) {
Assert.AreNotEqual(glCalculatorHelper.mpPtr, IntPtr.Zero);
}
}
#endregion

#region #isDisposed
[Test, GpuOnly]
public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() {
var glCalculatorHelper = new GlCalculatorHelper();

Assert.False(glCalculatorHelper.isDisposed);
using (var glCalculatorHelper = new GlCalculatorHelper()) {
Assert.False(glCalculatorHelper.isDisposed);
}
}

[Test, GpuOnly]
Expand All @@ -33,125 +33,133 @@ public class GlCalculatorHelperTest {
#region #InitializeForTest
[Test, GpuOnly]
public void InitializeForTest_ShouldInitialize() {
var glCalculatorHelper = new GlCalculatorHelper();

Assert.False(glCalculatorHelper.Initialized());
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
Assert.True(glCalculatorHelper.Initialized());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
Assert.False(glCalculatorHelper.Initialized());
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
Assert.True(glCalculatorHelper.Initialized());
}
}
#endregion

#region #RunInGlContext
[Test, GpuOnly]
public void RunInGlContext_ShouldReturnOk_When_FunctionReturnsOk() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var status = glCalculatorHelper.RunInGlContext(() => { return Status.Ok(); });
Assert.True(status.ok);
var status = glCalculatorHelper.RunInGlContext(() => { return Status.Ok(); });
Assert.True(status.ok);
}
}

[Test, GpuOnly]
public void RunInGlContext_ShouldReturnInternal_When_FunctionReturnsInternal() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var status = glCalculatorHelper.RunInGlContext(() => { return Status.Build(Status.StatusCode.Internal, "error"); });
Assert.AreEqual(status.code, Status.StatusCode.Internal);
var status = glCalculatorHelper.RunInGlContext(() => { return Status.Build(Status.StatusCode.Internal, "error"); });
Assert.AreEqual(status.code, Status.StatusCode.Internal);
}
}

[Test, GpuOnly]
public void RunInGlContext_ShouldReturnFailedPreCondition_When_FunctionThrows() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

GlCalculatorHelper.GlStatusFunction glStatusFunction = () => { throw new InvalidProgramException(); };
var status = glCalculatorHelper.RunInGlContext(glStatusFunction);
Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
GlCalculatorHelper.GlStatusFunction glStatusFunction = () => { throw new InvalidProgramException(); };
var status = glCalculatorHelper.RunInGlContext(glStatusFunction);
Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
}
}
#endregion

#region #CreateSourceTexture
[Test, GpuOnly]
public void CreateSourceTexture_ShouldReturnGlTexture_When_CalledWithImageFrame() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var imageFrame = new ImageFrame(ImageFormat.Format.SRGBA, 32, 24);
var status = glCalculatorHelper.RunInGlContext(() => {
var texture = glCalculatorHelper.CreateSourceTexture(imageFrame);
var imageFrame = new ImageFrame(ImageFormat.Format.SRGBA, 32, 24);
var status = glCalculatorHelper.RunInGlContext(() => {
var texture = glCalculatorHelper.CreateSourceTexture(imageFrame);
Assert.AreEqual(texture.width, 32);
Assert.AreEqual(texture.height, 24);
Assert.AreEqual(texture.width, 32);
Assert.AreEqual(texture.height, 24);
texture.Dispose();
return Status.Ok();
});
texture.Dispose();
return Status.Ok();
});

Assert.True(status.ok);
Assert.True(status.ok);
}
}

[Test, GpuOnly]
[Ignore("Skip because a thread hangs")]
public void CreateSourceTexture_ShouldFail_When_ImageFrameFormatIsInvalid() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 32, 24);
var status = glCalculatorHelper.RunInGlContext(() => {
using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame)) {
texture.Release();
}
return Status.Ok();
});

Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 32, 24);
var status = glCalculatorHelper.RunInGlContext(() => {
using (var texture = glCalculatorHelper.CreateSourceTexture(imageFrame)) {
texture.Release();
}
return Status.Ok();
});

Assert.AreEqual(status.code, Status.StatusCode.FailedPrecondition);
}
}
#endregion

#region #CreateDestinationTexture
[Test, GpuOnly]
public void CreateDestinationTexture_ShouldReturnGlTexture_When_GpuBufferFormatIsValid() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var status = glCalculatorHelper.RunInGlContext(() => {
var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32);
var status = glCalculatorHelper.RunInGlContext(() => {
var glTexture = glCalculatorHelper.CreateDestinationTexture(32, 24, GpuBufferFormat.kBGRA32);
Assert.AreEqual(glTexture.width, 32);
Assert.AreEqual(glTexture.height, 24);
return Status.Ok();
});
Assert.AreEqual(glTexture.width, 32);
Assert.AreEqual(glTexture.height, 24);
return Status.Ok();
});

Assert.True(status.ok);
Assert.True(status.ok);
}
}
#endregion

#region #framebuffer
[Test, GpuOnly]
public void framebuffer_ShouldReturnGLName() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

// default frame buffer
Assert.AreEqual(glCalculatorHelper.framebuffer, 0);
// default frame buffer
Assert.AreEqual(glCalculatorHelper.framebuffer, 0);
}
}
#endregion

#region #GetGlContext
[Test, GpuOnly]
public void GetGlContext_ShouldReturnCurrentContext() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var glContext = glCalculatorHelper.GetGlContext();
var glContext = glCalculatorHelper.GetGlContext();
#if UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero);
Assert.AreNotEqual(glContext.eglContext, IntPtr.Zero);
#elif UNITY_STANDALONE_OSX
Assert.AreNotEqual(glContext.nsglContext, IntPtr.Zero);
Assert.AreNotEqual(glContext.nsglContext, IntPtr.Zero);
#elif UNITY_IOS
Assert.AreNotEqual(glContext.eaglContext, IntPtr.Zero);
Assert.AreNotEqual(glContext.eaglContext, IntPtr.Zero);
#endif
}
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ public class GlContextTest {

[Test, GpuOnly]
public void GetCurrent_ShouldReturnCurrentContext_When_CalledInGlContext() {
var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

glCalculatorHelper.RunInGlContext(() => {
var glContext = GlContext.GetCurrent();
glCalculatorHelper.RunInGlContext(() => {
var glContext = GlContext.GetCurrent();
Assert.NotNull(glContext);
Assert.True(glContext.IsCurrent());
return Status.Ok();
}).AssertOk();
Assert.NotNull(glContext);
Assert.True(glContext.IsCurrent());
return Status.Ok();
}).AssertOk();
}
}
#endregion

Expand Down Expand Up @@ -56,17 +57,11 @@ public class GlContextTest {
#endregion

private GlContext GetGlContext() {
GlContext glContext = null;
using (var glCalculatorHelper = new GlCalculatorHelper()) {
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

var glCalculatorHelper = new GlCalculatorHelper();
glCalculatorHelper.InitializeForTest(GpuResources.Create().Value());

glCalculatorHelper.RunInGlContext(() => {
glContext = GlContext.GetCurrent();
return Status.Ok();
}).AssertOk();

return glContext;
return glCalculatorHelper.GetGlContext();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ public class GpuResourcesTest {
#region Create
[Test, GpuOnly]
public void Create_ShouldReturnStatusOrGpuResources() {
var statusOrGpuResources = GpuResources.Create();

Assert.True(statusOrGpuResources.ok);
using (var statusOrGpuResources = GpuResources.Create()) {
Assert.True(statusOrGpuResources.ok);
}
}
#endregion

#region #isDisposed
[Test, GpuOnly]
public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() {
var gpuResources = GpuResources.Create().Value();

Assert.False(gpuResources.isDisposed);
using (var gpuResources = GpuResources.Create().Value()) {
Assert.False(gpuResources.isDisposed);
}
}

[Test, GpuOnly]
Expand Down

0 comments on commit 9c3db9a

Please sign in to comment.