Ejemplo n.º 1
0
func TestSharedBuffer(t *testing.T) {
	var h0, h1 system.SharedBufferHandle
	var buf []byte
	var r system.MojoResult

	if r, h0 = core.CreateSharedBuffer(nil, 100); r != system.MOJO_RESULT_OK {
		t.Fatalf("CreateSharedBuffer failed:%v", r)
	}
	if !h0.IsValid() {
		t.Fatalf("CreateSharedBuffer returned an invalid handle h0:%v", h0)
	}
	if r, buf = h0.MapBuffer(0, 100, system.MOJO_MAP_BUFFER_FLAG_NONE); r != system.MOJO_RESULT_OK {
		t.Fatalf("MapBuffer failed to map buffer with h0:%v", r)
	}
	if len(buf) != 100 || cap(buf) != 100 {
		t.Fatalf("Buffer length(%d) and capacity(%d) should be %d", len(buf), cap(buf), 100)
	}
	buf[50] = 'x'
	if r, h1 = h0.DuplicateBufferHandle(nil); r != system.MOJO_RESULT_OK {
		t.Fatalf("DuplicateBufferHandle of h0 failed:%v", r)
	}
	if !h1.IsValid() {
		t.Fatalf("DuplicateBufferHandle returned an invalid handle h1:%v", h1)
	}
	if r = h0.Close(); r != system.MOJO_RESULT_OK {
		t.Fatalf("Close on h0 failed:%v", r)
	}
	buf[51] = 'y'
	if r = h1.UnmapBuffer(buf); r != system.MOJO_RESULT_OK {
		t.Fatalf("UnmapBuffer failed:%v", r)
	}
	if r, buf = h1.MapBuffer(50, 50, system.MOJO_MAP_BUFFER_FLAG_NONE); r != system.MOJO_RESULT_OK {
		t.Fatalf("MapBuffer failed to map buffer with h1:%v", r)
	}
	if len(buf) != 50 || cap(buf) != 50 {
		t.Fatalf("Buffer length(%d) and capacity(%d) should be %d", len(buf), cap(buf), 50)
	}
	if buf[0] != 'x' || buf[1] != 'y' {
		t.Fatalf("Failed to validate shared buffer. expected:x,y got:%s,%s", buf[0], buf[1])
	}
	if r = h1.UnmapBuffer(buf); r != system.MOJO_RESULT_OK {
		t.Fatalf("UnmapBuffer failed:%v", r)
	}
	if r = h1.Close(); r != system.MOJO_RESULT_OK {
		t.Fatalf("Close on h1 failed:%v", r)
	}
}
Ejemplo n.º 2
0
func TestHandle(t *testing.T) {
	var handle system.SharedBufferHandle
	var r system.MojoResult

	if r, handle = core.CreateSharedBuffer(nil, 1); r != system.MOJO_RESULT_OK {
		t.Fatalf("CreateSharedBuffer failed:%v", r)
	}
	if !handle.IsValid() {
		t.Fatalf("CreateSharedBuffer returned invalid handle:%v", handle)
	}
	duplicate := handle
	if duplicate.NativeHandle() != handle.NativeHandle() {
		t.Fatalf("duplicate(%v) and handle(%v) point to different handles", duplicate.NativeHandle(), handle.NativeHandle())
	}
	releasedHandle := handle.ReleaseNativeHandle()
	if duplicate.IsValid() || handle.IsValid() {
		t.Fatalf("duplicate(%v) and handle(%v) should be invalid after releasing native handle", duplicate.NativeHandle(), handle.NativeHandle())
	}
	handle = core.AcquireNativeHandle(releasedHandle).ToSharedBufferHandle()
	if handle.NativeHandle() != releasedHandle || !handle.IsValid() {
		t.Fatalf("handle(%v) should be valid after AcquireNativeHandle", handle.NativeHandle())
	}
	untypedHandle := handle.ToUntypedHandle()
	if handle.IsValid() {
		t.Fatalf("handle(%v) should be invalid after call ToUntypedHandle", handle.NativeHandle())
	}
	handle = untypedHandle.ToSharedBufferHandle()
	if untypedHandle.IsValid() {
		t.Fatalf("untypedHandle(%v) should be invalid after call ToSharedBufferHandle", untypedHandle.NativeHandle())
	}
	if handle.NativeHandle() != releasedHandle {
		t.Fatalf("handle(%v) should be wrapping %v", handle.NativeHandle(), releasedHandle)
	}
	if r = handle.Close(); r != system.MOJO_RESULT_OK {
		t.Fatalf("Close on handle failed:%v", r)
	}
}