Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astrada committed Jan 20, 2021
1 parent 35292b9 commit 3bbc4d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.14.2
version=0.16.0
profile=conventional
21 changes: 14 additions & 7 deletions test/testBufferPool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,40 @@ let test_acquire_buffer () =
let buffer_pool = BufferPool.create ~pool_size:10 ~buffer_size:10 in
let mutex = Mutex.create () in
let condition = Condition.create () in
let buffer = BufferPool.acquire_buffer mutex condition buffer_pool in
buffer.BufferPool.Buffer.arr.{0} <- 'a';
BufferPool.release_buffer buffer condition buffer_pool;
let buffer' = BufferPool.acquire_buffer mutex condition buffer_pool in
assert_equal ~printer:Std.string_of_char 'a' buffer'.BufferPool.Buffer.arr.{0}
Utils.with_lock mutex (fun () ->
let buffer = BufferPool.acquire_buffer mutex condition buffer_pool in
buffer.BufferPool.Buffer.arr.{0} <- 'a';
BufferPool.release_buffer buffer condition buffer_pool;
let buffer' = BufferPool.acquire_buffer mutex condition buffer_pool in
assert_equal ~printer:Std.string_of_char 'a'
buffer'.BufferPool.Buffer.arr.{0})

let test_pending_requests () =
let flag = ref false in
let buffer_pool = BufferPool.create ~pool_size:10 ~buffer_size:10 in
let mutex = Mutex.create () in
let condition = Condition.create () in
Mutex.lock mutex;
let buffer = BufferPool.acquire_buffer mutex condition buffer_pool in
assert_equal ~printer:string_of_int 0
(BufferPool.pending_requests buffer_pool);
buffer.BufferPool.Buffer.arr.{0} <- 'b';
Mutex.unlock mutex;
let thread =
Thread.create
(fun () ->
let b = BufferPool.acquire_buffer mutex condition buffer_pool in
flag := b.BufferPool.Buffer.arr.{0} = 'a')
Utils.with_lock mutex (fun () ->
let b = BufferPool.acquire_buffer mutex condition buffer_pool in
flag := b.BufferPool.Buffer.arr.{0} = 'a'))
()
in
Thread.delay 0.05;
assert_equal ~printer:string_of_int 1
(BufferPool.pending_requests buffer_pool);
buffer.BufferPool.Buffer.arr.{0} <- 'a';
Mutex.lock mutex;
BufferPool.release_buffer buffer condition buffer_pool;
Mutex.unlock mutex;
Thread.join thread;
assert_equal ~printer:string_of_int 0
(BufferPool.pending_requests buffer_pool);
Expand Down

0 comments on commit 3bbc4d0

Please sign in to comment.