public void ImageMemoryTest() { SetImageStubImplementation(); CallCount count = NativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); Task.Run(() => { using (Image image = new Image(ImageFormat.Custom, 640, 480, 640 * 2)) { Memory <byte> memory = image.Memory; System.Span <byte> memorySpan = memory.Span; System.Span <short> shortSpan = MemoryMarshal.Cast <byte, short>(memorySpan); Assert.AreEqual(0, shortSpan[0]); Assert.AreEqual(1, shortSpan[1]); image.Dispose(); } }).Wait(); GC.Collect(0, GCCollectionMode.Forced, true, true); GC.WaitForPendingFinalizers(); Assert.AreEqual(count.Calls("k4a_image_reference") + 1, count.Calls("k4a_image_release"), "References not zero"); }
public void Temperature() { this.SetCaptureReleaseImplementation(); this.nativeK4a.SetImplementation(@" float k4a_capture_get_temperature_c(k4a_capture_t capture_handle) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); return 2.5f; } void k4a_capture_set_temperature_c(k4a_capture_t capture_handle, float value) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); STUB_ASSERT(value == 3.5f); } "); CallCount count = this.nativeK4a.CountCalls(); using (Capture c = new Capture()) { // Temperature values should not be cached, so every access should call the // native layer Assert.AreEqual(0, count.Calls("k4a_capture_get_temperature_c")); Assert.AreEqual(2.5f, c.Temperature); Assert.AreEqual(1, count.Calls("k4a_capture_get_temperature_c")); Assert.AreEqual(2.5f, c.Temperature); Assert.AreEqual(2, count.Calls("k4a_capture_get_temperature_c")); // Verify writes Assert.AreEqual(0, count.Calls("k4a_capture_set_temperature_c")); c.Temperature = 3.5f; Assert.AreEqual(1, count.Calls("k4a_capture_set_temperature_c")); // Verify the write is being marshaled correctly _ = Assert.Throws(typeof(NativeFailureException), () => { c.Temperature = 4.0f; }); c.Dispose(); // Verify disposed behavior _ = Assert.Throws(typeof(ObjectDisposedException), () => { c.Temperature = 4.0f; }); _ = Assert.Throws(typeof(ObjectDisposedException), () => { float x = c.Temperature; }); } }
public void CaptureGarbageCollection() { this.nativeK4a.SetImplementation(@" k4a_result_t k4a_capture_create(k4a_capture_t* capture_handle) { STUB_ASSERT(capture_handle != NULL); *capture_handle = (k4a_capture_t)0x0C001234; return K4A_RESULT_SUCCEEDED; } void k4a_capture_release(k4a_capture_t capture_handle) { STUB_ASSERT(capture_handle == (k4a_capture_t)0x0C001234); } k4a_result_t k4a_set_debug_message_handler( k4a_logging_message_cb_t *message_cb, void *message_cb_context, k4a_log_level_t min_level) { STUB_ASSERT(message_cb != NULL); return K4A_RESULT_SUCCEEDED; } "); CallCount count = this.nativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_capture_create")); Assert.AreEqual(0, count.Calls("k4a_capture_release")); WeakReference capture = this.CreateWithWeakReference(() => { Capture c = new Capture(); // The reference should still exist and we should have not seen close called Assert.AreEqual(1, count.Calls("k4a_capture_create")); Assert.AreEqual(0, count.Calls("k4a_capture_release")); return(c); }); // The reference to the Capture object is no longer on the stack, and therefore is free to be garbage collected // At this point capture.IsAlive is likely to be true, but not guaranteed to be // Force garbage collection GC.Collect(0, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); Assert.AreEqual(false, capture.IsAlive); // k4a_capture_release should have been called automatically Assert.AreEqual(1, count.Calls("k4a_capture_create")); Assert.AreEqual(1, count.Calls("k4a_capture_release")); }
public void TestProbeCreator_Create_OnlyCreatesOneTestProbe() { //arrange TestProbeCreator sut = CreateTestProbeCreator(); //act sut.Create(TestKitBase); //assert CallCount.Should().Be(1); }
public void ModuleTest() { RunTest(context => { var script = @" const self = this; function enter() { console.log('enter()'); self.doThing(); } function update() { console.log('update()'); } function exit() { console.log('exit'); } function msgMissing() { console.log('msgMissing'); } if (typeof module !== 'undefined') { module.exports = { enter: enter, update: update, exit: exit, msgMissing: msgMissing }; }"; var cc = new CallCount(); var module = context.NewModule("module_1234"); context.RunScript(string.Empty, cc, script, module); var fn = module.GetExportedValue <IJsCallback>("enter"); fn.Invoke(); }); }
private CallCount GetOnQueueActionCallCount(DurableJobQueueActionType filter, int maxConcurrent, int jobsToCreate, int releaseAfterJobCountStateChanged) { var scheduler = new HistoricalScheduler(); var queuedEvent = new ManualResetEventSlim(false); var callCount = new CallCount() { MonitoredQueue = _monitoredJobQueueFactory(scheduler, _durableQueueFactory), }; callCount.MonitoredQueue.MaxConcurrent = maxConcurrent; using (var subscription = callCount.MonitoredQueue.OnQueueAction .Subscribe(action => { if (action.ActionType == filter) { ++callCount.Actual; if (callCount.Actual == releaseAfterJobCountStateChanged) { queuedEvent.Set(); } //else if (callCount.Actual == callCount.MonitoredQueue.MaxQueueItemsToPublishPerInterval) // queuedEvent.Set(); } })) { foreach (var input in _fixture.CreateMany <TInput>(jobsToCreate)) { callCount.MonitoredQueue.AddJob(input); } scheduler.AdvanceBy(callCount.MonitoredQueue.PollingInterval.Add(TimeSpan.FromSeconds(1))); queuedEvent.Wait(TimeSpan.FromSeconds(5)); return(callCount); } }
public void ImageGarbageCollection() { SetImageStubImplementation(); CallCount count = NativeK4a.CountCalls(); Assert.AreEqual(0, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); System.WeakReference image = CreateWithWeakReference(() => { Image i = new Image(ImageFormat.Custom, 640, 480, 640 * 2); var memory = i.Memory; // The reference should still exist and we should have not seen close called Assert.AreEqual(1, count.Calls("k4a_image_create")); Assert.AreEqual(0, count.Calls("k4a_image_release")); return(i); }); // The reference to the Device object is no longer on the stack, and therefore is free to be garbage collected // At this point capture.IsAlive is likely to be true, but not garanteed to be // Force garbage collection System.GC.Collect(0, System.GCCollectionMode.Forced, true); System.GC.WaitForPendingFinalizers(); Assert.AreEqual(false, image.IsAlive); // k4a_device_close should have been called automatically Assert.AreEqual(1, count.Calls("k4a_image_create")); Assert.AreEqual(count.Calls("k4a_image_reference") + 1, count.Calls("k4a_image_release")); }
public void ImageProperties() { this.SetCaptureReleaseImplementation(); this.SetImageMockImplementation(); this.nativeK4a.SetImplementation(@" k4a_image_t color = 0; k4a_image_t depth = 0; k4a_image_t ir = 0; k4a_image_t k4a_capture_get_color_image(k4a_capture_t capture_handle) { return color; } void k4a_capture_set_color_image(k4a_capture_t capture_handle, k4a_image_t image) { color = image; } k4a_image_t k4a_capture_get_depth_image(k4a_capture_t capture_handle) { return depth; } void k4a_capture_set_depth_image(k4a_capture_t capture_handle, k4a_image_t image) { depth = image; } k4a_image_t k4a_capture_get_ir_image(k4a_capture_t capture_handle) { return ir; } void k4a_capture_set_ir_image(k4a_capture_t capture_handle, k4a_image_t image) { ir = image; } "); CallCount count = this.nativeK4a.CountCalls(); // Verify we can create and destroy using the default constructor using (Capture c = new Capture()) { } using (Capture c = new Capture()) { using (Capture captureReference = c.Reference()) { // Verify we have two capture objects that represent the same // native object Assert.IsTrue(c.NativeEquals(captureReference)); Assert.AreNotSame(c, captureReference); Image image1 = new Image(ImageFormat.ColorBGRA32, 10, 10); Image image1_reference = image1.Reference(); Image image2 = new Image(ImageFormat.ColorBGRA32, 10, 10); Image image2_reference = image2.Reference(); Assert.IsFalse(image1.NativeEquals(image2)); Assert.AreNotSame(image1, image2); // Verify our images represent the same native object, but are not the same managed wrapper Assert.IsTrue(image1.NativeEquals(image1_reference)); Assert.AreNotSame(image1, image1_reference); Assert.IsTrue(image2.NativeEquals(image2_reference)); Assert.AreNotSame(image2, image2_reference); Assert.IsNull(c.Color); // Assign image1 to the capture, it is now owned by the capture object c.Color = image1; // both captures should refer to the same image Assert.IsTrue(image1.NativeEquals(c.Color)); Assert.IsFalse(image2.NativeEquals(c.Color)); Assert.IsTrue(image1.NativeEquals(captureReference.Color)); Assert.IsFalse(image2.NativeEquals(captureReference.Color)); // The reference should have its own wrapper that represents // the same native image Assert.IsTrue(c.Color.NativeEquals(captureReference.Color)); Assert.AreNotSame(c.Color, captureReference.Color); c.Color = image2; // By re-assigning the image, the capture will dispose the original image _ = Assert.Throws(typeof(ObjectDisposedException), () => { _ = image1.Size; }); // Verify that the capture is now referring to image2. Assert.IsTrue(image2.NativeEquals(c.Color)); Assert.AreSame(image2, c.Color); // Verify that the reference to the capture also is updated to the same new image Assert.IsFalse(image1_reference.NativeEquals(captureReference.Color)); Assert.IsTrue(image2.NativeEquals(captureReference.Color)); // The second capture will have its own wrapper Assert.AreNotSame(image2, captureReference.Color); // Assign the depth and IR properties to the same image c.Depth = image2; c.IR = image2; Assert.AreSame(image2, c.Depth); Assert.AreSame(image2, c.IR); } } }
public void ModuleTest() { RunTest(context => { var script = @" const self = this; function enter() { console.log('enter()'); self.doThing(); } function update() { console.log('update()'); } function exit() { nothing.error } function msgMissing() { console.log('msgMissing'); } function buildCallback() { return function() { nothing.error }; } if (typeof module !== 'undefined') { module.exports = { enter: enter, update: update, exit: exit, msgMissing: msgMissing, buildCallback: buildCallback }; }"; var cc = new CallCount(); var moduleName = "TestModule"; var module = context.NewModule("module_1234"); context.RunScript(moduleName, cc, script, module); var fnEnter = module.GetExportedValue <IJsCallback>("enter"); fnEnter.Invoke(); Assert.IsNull(fnEnter.ExecutionError); var fnExit = module.GetExportedValue <IJsCallback>("exit"); fnExit.Invoke(); Assert.NotNull(fnExit.ExecutionError); Assert.AreEqual(moduleName, ((JavaScriptException)fnExit.ExecutionError).Location.Source); }); }