Skip to content

Commit

Permalink
using CountDownEvent in request sender test
Browse files Browse the repository at this point in the history
Change-Id: I9470510c8af391f04e9a977fe909a732f244ebef
  • Loading branch information
wu-hanqing authored and bai-charisu committed Aug 12, 2020
1 parent 096a1ad commit 111240e
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions test/client/request_sender_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "src/client/client_common.h"
#include "src/client/request_sender.h"
#include "src/common/concurrent/count_down_event.h"
#include "test/client/mock_chunkservice.h"

namespace curve {
Expand All @@ -42,19 +43,26 @@ using ::testing::SaveArgPointee;
using ::testing::SetArgPointee;
using ::testing::SetArgReferee;

using curve::common::CountDownEvent;

class FakeChunkClosure : public ClientClosure {
public:
FakeChunkClosure()
: ClientClosure(nullptr, nullptr), reqeustClosure(nullptr) {
explicit FakeChunkClosure(CountDownEvent* event)
: ClientClosure(nullptr, nullptr),
reqeustClosure(nullptr),
event(event) {
SetClosure(&reqeustClosure);
}

void Run() override {}
void Run() override {
event->Signal();
}

void SendRetryRequest() override {}

private:
RequestClosure reqeustClosure;
CountDownEvent* event;
};

void MockChunkRequestService(::google::protobuf::RpcController* controller,
Expand Down Expand Up @@ -116,14 +124,14 @@ TEST_F(RequestSenderTest, TestReadChunkAppliedIndex) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

appliedIndex = 100;
requestSender.ReadChunk(ChunkIDInfo(), 0, 0, 0, appliedIndex, {},
&closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_TRUE(chunkRequest.has_appliedindex());
}

Expand All @@ -134,14 +142,14 @@ TEST_F(RequestSenderTest, TestReadChunkAppliedIndex) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

appliedIndex = 0;
requestSender.ReadChunk(ChunkIDInfo(), 0, 0, 0, appliedIndex, {},
&closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_FALSE(chunkRequest.has_appliedindex());
}
}
Expand All @@ -162,14 +170,14 @@ TEST_F(RequestSenderTest, TestWriteChunkSourceInfo) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

sourceInfo.cloneFileSource.clear();
requestSender.WriteChunk(ChunkIDInfo(), 0, 0, 0, 0,
sourceInfo, &closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_FALSE(chunkRequest.has_clonefilesource());
ASSERT_FALSE(chunkRequest.has_clonefileoffset());
}
Expand All @@ -181,16 +189,16 @@ TEST_F(RequestSenderTest, TestWriteChunkSourceInfo) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

sourceInfo.cloneFileSource = "/test_WriteChunkSourceInfo";
sourceInfo.cloneFileOffset = 0;

requestSender.WriteChunk(ChunkIDInfo(), 0, 0, 0, 0,
sourceInfo, &closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_TRUE(chunkRequest.has_clonefilesource());
ASSERT_TRUE(chunkRequest.has_clonefileoffset());
}
Expand All @@ -215,14 +223,14 @@ TEST_F(RequestSenderTest, TestReadChunkSourceInfo) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

sourceInfo.cloneFileSource.clear();
requestSender.ReadChunk(ChunkIDInfo(), 0, 0, 0, appliedIndex,
sourceInfo, &closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_FALSE(chunkRequest.has_clonefilesource());
ASSERT_FALSE(chunkRequest.has_clonefileoffset());
}
Expand All @@ -234,16 +242,16 @@ TEST_F(RequestSenderTest, TestReadChunkSourceInfo) {
.WillOnce(DoAll(SaveArgPointee<1>(&chunkRequest),
Invoke(MockChunkRequestService)));

FakeChunkClosure closure;
CountDownEvent event(1);
FakeChunkClosure closure(&event);

sourceInfo.cloneFileSource = "/test_ReadChunkSourceInfo";
sourceInfo.cloneFileOffset = 0;

requestSender.ReadChunk(ChunkIDInfo(), 0, 0, 0, appliedIndex,
sourceInfo, &closure);

std::this_thread::sleep_for(std::chrono::milliseconds(5));

event.Wait();
ASSERT_TRUE(chunkRequest.has_clonefilesource());
ASSERT_TRUE(chunkRequest.has_clonefileoffset());
}
Expand Down

0 comments on commit 111240e

Please sign in to comment.