From 3bbc4d054fbd7a7cdef36c17d88a5316183fd403 Mon Sep 17 00:00:00 2001 From: Alessandro Strada Date: Wed, 20 Jan 2021 23:33:14 +0100 Subject: [PATCH] Fix tests --- .ocamlformat | 2 +- test/testBufferPool.ml | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.ocamlformat b/.ocamlformat index ae254d9..e4c0f5d 100644 --- a/.ocamlformat +++ b/.ocamlformat @@ -1,2 +1,2 @@ -version=0.14.2 +version=0.16.0 profile=conventional diff --git a/test/testBufferPool.ml b/test/testBufferPool.ml index 3bc6768..beaf67a 100644 --- a/test/testBufferPool.ml +++ b/test/testBufferPool.ml @@ -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);