Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/TimedDoor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TimedDoor : public Door {
bool isOpened;
public:
explicit TimedDoor(int);
~TimedDoor();
bool isDoorOpened();
void unlock();
void lock();
Expand Down
59 changes: 59 additions & 0 deletions src/TimedDoor.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
// Copyright 2021 GHA Test Team
#include "TimedDoor.h"

#include <algorithm>
#include <chrono>
#include <stdexcept>
#include <thread>

DoorTimerAdapter::DoorTimerAdapter(TimedDoor& timedDoor) : door(timedDoor) {}

void DoorTimerAdapter::Timeout() {
door.throwState();
}

TimedDoor::TimedDoor(int timeout)
: adapter(new DoorTimerAdapter(*this)),
iTimeout(timeout),
isOpened(false) {}

TimedDoor::~TimedDoor() {
delete adapter;
}

bool TimedDoor::isDoorOpened() {
return isOpened;
}

void TimedDoor::unlock() {
isOpened = true;

Timer timer;
timer.tregister(iTimeout, adapter);
}

void TimedDoor::lock() {
isOpened = false;
}

int TimedDoor::getTimeOut() const {
return iTimeout;
}

void TimedDoor::throwState() {
if (isOpened) {
throw std::runtime_error("Door is open past allowed timeout");
}
}

void Timer::sleep(int timeout) {
auto ms = std::chrono::milliseconds(std::max(0, timeout));
std::this_thread::sleep_for(ms);
}

void Timer::tregister(int timeout, TimerClient* timerClient) {
client = timerClient;
sleep(timeout);

if (client != nullptr) {
client->Timeout();
}
}
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// Copyright 2021 GHA Test Team
#include "TimedDoor.h"

#include <exception>
#include <iostream>

int main() {
TimedDoor tDoor(5);
tDoor.lock();
tDoor.unlock();
try {
tDoor.unlock();
} catch (const std::exception& error) {
std::cout << error.what() << std::endl;
}

return 0;
}
35 changes: 22 additions & 13 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
include(FetchContent)
find_package(GTest QUIET)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.0
)
FetchContent_MakeAvailable(googletest)
if(NOT GTest_FOUND)
include(FetchContent)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.0
)
FetchContent_MakeAvailable(googletest)
endif()

include_directories(${gtest_SOURCE_DIR}/include)
message(PROJECT_SOURCE_DIR="${gtest_SOURCE_DIR}/include")
include_directories(${${PROJECT_NAME}_SOURCE_DIR}/include)
set(header ${${PROJECT_NAME}_SOURCE_DIR}/include)

add_executable(${PROJECT_NAME}.test
AllTests.cpp
tests.cpp)

target_link_libraries(${PROJECT_NAME}.test
if(TARGET GTest::gtest AND TARGET GTest::gmock)
target_link_libraries(${PROJECT_NAME}.test
${PROJECT_NAME}
GTest::gtest
GTest::gmock
)
else()
target_link_libraries(${PROJECT_NAME}.test
${PROJECT_NAME}
gtest
gmock
)
)
endif()

install(TARGETS ${PROJECT_NAME}.test
DESTINATION "${tool_dest}")
DESTINATION "${tool_dest}")
Loading
Loading