Skip to content

Commit 1168e90

Browse files
Use IEnvironment in WaterPumpScheduler to get time, instead of passing it in as a parameter.
1 parent 34634eb commit 1168e90

File tree

8 files changed

+58
-42
lines changed

8 files changed

+58
-42
lines changed

controller/tea_poor/lib/CommandProcessor/CommandProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ std::string CommandProcessor::pour_tea(const char *milliseconds, const char *pow
5454
return std::string("{ \"error\": \"invalid power value\" }");
5555
}
5656
// start pouring tea
57-
_waterPump->start( atoi(milliseconds), atoi(power), _env->time() );
57+
_waterPump->start( atoi(milliseconds), atoi(power) );
5858
return status();
5959
}
6060

controller/tea_poor/lib/WaterPumpScheduler/WaterPumpScheduler.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "WaterPumpScheduler.h"
22

3-
WaterPumpScheduler::WaterPumpScheduler(IWaterPumpPtr waterPump, unsigned long forceStopIntervalMs) :
3+
WaterPumpScheduler::WaterPumpScheduler(IWaterPumpPtr waterPump, IEnvironmentPtr env, unsigned long forceStopIntervalMs) :
44
_waterPump(waterPump),
5+
_env(env),
56
_forceStopIntervalMs(forceStopIntervalMs)
67
{
78
}
@@ -12,8 +13,8 @@ void WaterPumpScheduler::setup() {
1213
_waterPump->setup();
1314
}
1415

15-
void WaterPumpScheduler::start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) {
16-
_stopTime = currentTimeMs + runTimeMs;
16+
void WaterPumpScheduler::start(unsigned long runTimeMs, int power) {
17+
_stopTime = _env->time() + runTimeMs;
1718
_waterPump->start(power);
1819
}
1920

@@ -22,7 +23,8 @@ void WaterPumpScheduler::stop() {
2223
_stopTime = 0; // a bit of paranoia :)
2324
}
2425

25-
void WaterPumpScheduler::tick(unsigned long currentTimeMs) {
26+
void WaterPumpScheduler::tick() {
27+
const auto currentTimeMs = _env->time();
2628
if (_stopTime <= currentTimeMs) {
2729
stop();
2830
_stopTime = currentTimeMs + _forceStopIntervalMs; // force stop after X milliseconds

controller/tea_poor/lib/WaterPumpScheduler/WaterPumpScheduler.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33

44
#include <IWaterPump.h>
55
#include <IWaterPumpSchedulerAPI.h>
6+
#include <IEnvironment.h>
67

78
// This class is responsible for scheduling water pump
89
// It is used to make sure that water pump is running for a limited time
910
// It is also ensuring that water pump is stopped if not needed
1011
class WaterPumpScheduler : public IWaterPumpSchedulerAPI {
1112
private:
1213
IWaterPumpPtr _waterPump;
14+
IEnvironmentPtr _env;
1315
unsigned long _stopTime = 0;
1416
// each X milliseconds will force stop water pump
1517
unsigned long _forceStopIntervalMs;
1618
public:
17-
WaterPumpScheduler(IWaterPumpPtr waterPump, unsigned long forceStopIntervalMs);
18-
WaterPumpScheduler(IWaterPumpPtr waterPump) : WaterPumpScheduler(waterPump, 1000) {}
19+
WaterPumpScheduler(IWaterPumpPtr waterPump, IEnvironmentPtr env, unsigned long forceStopIntervalMs);
20+
// forceStopIntervalMs is set to 1000ms by default
21+
WaterPumpScheduler(IWaterPumpPtr waterPump, IEnvironmentPtr env) : WaterPumpScheduler(waterPump, env, 1000) {}
1922
~WaterPumpScheduler();
2023

2124
void setup();
2225
// for simplicity and testability we are passing current time as parameter
23-
void tick(unsigned long currentTimeMs);
26+
void tick();
2427

2528
// Public API
26-
void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) override;
29+
void start(unsigned long runTimeMs, int power) override;
2730
void stop() override;
2831
WaterPumpStatus status() override;
2932
};

controller/tea_poor/lib/interfaces/IWaterPumpSchedulerAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IWaterPumpSchedulerAPI {
2222
public:
2323
virtual ~IWaterPumpSchedulerAPI() {}
2424
virtual void stop() = 0;
25-
virtual void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) = 0;
25+
virtual void start(unsigned long runTimeMs, int power) = 0;
2626
virtual WaterPumpStatus status() = 0;
2727
};
2828

controller/tea_poor/src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#include <sstream>
1010
#include <ArduinoEnvironment.h>
1111

12-
IEnvironmentPtr env = std::make_shared<ArduinoEnvironment>();
12+
const IEnvironmentPtr env = std::make_shared<ArduinoEnvironment>();
1313

1414
// Setting up water pump
15-
auto waterPump = std::make_shared<WaterPumpScheduler>(
15+
const auto waterPump = std::make_shared<WaterPumpScheduler>(
1616
std::make_shared<WaterPumpController>(
1717
WATER_PUMP_DIRECTION_PIN, WATER_PUMP_BRAKE_PIN, WATER_PUMP_POWER_PIN
18-
)
18+
), env
1919
);
2020

2121
// build command processor
@@ -76,6 +76,6 @@ void setup() {
7676
}
7777

7878
void loop() {
79-
waterPump->tick(millis());
79+
waterPump->tick();
8080
remoteControl.process();
8181
};

controller/tea_poor/test/test_native/tests/CommandProcessor_test.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ TEST(CommandProcessor, pour_tea_invalid_power) {
3434

3535
// for simplicity of the UI, we should accept as valid 0 and exactly threshold value
3636
TEST(CommandProcessor, pour_tea_valid_boundary_values) {
37-
auto env = std::make_shared<FakeEnvironment>();
38-
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
37+
const auto env = std::make_shared<FakeEnvironment>();
38+
const auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>(env);
3939
CommandProcessor commandProcessor(123, env, waterPump);
4040

4141
ASSERT_NE(commandProcessor.pour_tea("0", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
@@ -44,27 +44,27 @@ TEST(CommandProcessor, pour_tea_valid_boundary_values) {
4444

4545
// test that start pouring tea by calling pour_tea() method with specified parameters
4646
TEST(CommandProcessor, pour_tea) {
47-
auto env = std::make_shared<FakeEnvironment>();
47+
const auto env = std::make_shared<FakeEnvironment>();
48+
const auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>(env);
4849
env->time(2343);
49-
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
5050
CommandProcessor commandProcessor(10000, env, waterPump);
5151
const auto response = commandProcessor.pour_tea("1234", "23");
5252
ASSERT_EQ(waterPump->_log, "start(1234, 23, 2343)\n");
5353
}
5454

5555
// test that stop() method stops pouring tea
5656
TEST(CommandProcessor, stop) {
57-
auto env = std::make_shared<FakeEnvironment>();
58-
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
57+
const auto env = std::make_shared<FakeEnvironment>();
58+
const auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>(env);
5959
CommandProcessor commandProcessor(123, env, waterPump);
6060
const auto response = commandProcessor.stop();
6161
ASSERT_EQ(waterPump->_log, "stop()\n");
6262
}
6363

6464
// test that status() method returns JSON string with water pump status
6565
TEST(CommandProcessor, status) {
66-
auto env = std::make_shared<FakeEnvironment>();
67-
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
66+
const auto env = std::make_shared<FakeEnvironment>();
67+
const auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>(env);
6868
CommandProcessor commandProcessor(123, env, waterPump);
6969
const auto response = commandProcessor.status();
7070
ASSERT_EQ(response, "{"
@@ -80,8 +80,8 @@ TEST(CommandProcessor, status) {
8080

8181
// test that status() method returns JSON string with actual time left
8282
TEST(CommandProcessor, status_running) {
83-
auto env = std::make_shared<FakeEnvironment>();
84-
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
83+
const auto env = std::make_shared<FakeEnvironment>();
84+
const auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>(env);
8585
CommandProcessor commandProcessor(12345, env, waterPump);
8686

8787
commandProcessor.pour_tea("1123", "100");
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
11
#include <gtest/gtest.h>
22
#include "mocks/FakeWaterPump.h"
3+
#include "mocks/FakeEnvironment.h"
34
#include <WaterPumpScheduler.h>
45

56
// test that pump is stopping after given time
67
TEST(WaterPumpScheduler, test_pump_stops_after_given_time) {
78
// random time between 1 and 10 seconds
89
const unsigned long runTimeMs = 1000 + (rand() % 10) * 1000;
10+
const auto fakeEnvironment = std::make_shared<FakeEnvironment>();
911
const auto fakeWaterPump = std::make_shared<FakeWaterPump>();
10-
WaterPumpScheduler waterPumpScheduler(fakeWaterPump);
12+
WaterPumpScheduler waterPumpScheduler(fakeWaterPump, fakeEnvironment);
1113
waterPumpScheduler.setup();
1214
// start water pump
13-
unsigned long currentTimeMs = 0;
14-
waterPumpScheduler.start(runTimeMs, 1, currentTimeMs);
15+
fakeEnvironment->time(0);
16+
waterPumpScheduler.start(runTimeMs, 1);
1517
ASSERT_EQ(fakeWaterPump->powerInPercents(), 1);
1618
// check status
1719
auto status = waterPumpScheduler.status();
1820
ASSERT_TRUE(status.isRunning);
1921
ASSERT_EQ(status.stopTime, runTimeMs);
2022

21-
while (currentTimeMs < runTimeMs) {
22-
waterPumpScheduler.tick(currentTimeMs);
23+
while (fakeEnvironment->time() < runTimeMs) {
24+
waterPumpScheduler.tick();
2325
ASSERT_TRUE(fakeWaterPump->isRunning());
24-
currentTimeMs += 100;
26+
fakeEnvironment->time(fakeEnvironment->time() + 100);
2527
}
2628
// pump should be stopped after given time
27-
waterPumpScheduler.tick(runTimeMs + 1);
29+
fakeEnvironment->time(runTimeMs + 1);
30+
waterPumpScheduler.tick();
2831
ASSERT_FALSE(fakeWaterPump->isRunning());
2932
}
3033

3134
// test that pump is periodically forced to stop after given time
3235
TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_time) {
3336
const auto fakeWaterPump = std::make_shared<FakeWaterPump>();
34-
WaterPumpScheduler waterPumpScheduler(fakeWaterPump, 1000); // force stop each 1 second
37+
const auto fakeEnvironment = std::make_shared<FakeEnvironment>();
38+
const int T = 1000; // 1 second
39+
WaterPumpScheduler waterPumpScheduler(fakeWaterPump, fakeEnvironment, T); // force stop each T
3540
waterPumpScheduler.setup();
3641
// start water pump
37-
unsigned long currentTimeMs = 0;
38-
waterPumpScheduler.start(1, 1, currentTimeMs);
39-
currentTimeMs += 1;
40-
waterPumpScheduler.tick(currentTimeMs);
42+
fakeEnvironment->time(0);
43+
waterPumpScheduler.start(1, 1);
44+
fakeEnvironment->time(1);
45+
waterPumpScheduler.tick();
4146
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped after given time
4247

4348
for(int i = 0; i < 10; i++) {
4449
// emulate that pump was started again
4550
fakeWaterPump->start(1);
4651
ASSERT_EQ(fakeWaterPump->powerInPercents(), 1);
47-
currentTimeMs += 1000;
48-
waterPumpScheduler.tick(currentTimeMs);
52+
fakeEnvironment->time(fakeEnvironment->time() + T);
53+
waterPumpScheduler.tick();
4954
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped
5055
}
5156
}
5257

5358
// test that pumps power is set to specified value
5459
TEST(WaterPumpScheduler, test_pumps_power_is_set_to_specified_value) {
5560
const auto fakeWaterPump = std::make_shared<FakeWaterPump>();
56-
WaterPumpScheduler waterPumpScheduler(fakeWaterPump);
61+
const auto fakeEnvironment = std::make_shared<FakeEnvironment>();
62+
WaterPumpScheduler waterPumpScheduler(fakeWaterPump, fakeEnvironment);
5763
waterPumpScheduler.setup();
5864
const int power = 23;
59-
waterPumpScheduler.start(1, power, 0);
65+
waterPumpScheduler.start(1, power);
6066
ASSERT_EQ(fakeWaterPump->powerInPercents(), power);
6167
}

controller/tea_poor/test/test_native/tests/mocks/FakeWaterPumpSchedulerAPI.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
#define FAKE_WATER_PUMP_SCHEDULER_API_H
44

55
#include <IWaterPumpSchedulerAPI.h>
6+
#include <IEnvironment.h>
67
#include <string>
78

89
class FakeWaterPumpSchedulerAPI : public IWaterPumpSchedulerAPI {
10+
private:
11+
const IEnvironmentPtr _env;
912
public:
13+
FakeWaterPumpSchedulerAPI(IEnvironmentPtr env) : _env(env) {}
14+
1015
void stop() override {
1116
_log += "stop()\n";
1217
}
1318

14-
void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) override {
19+
void start(unsigned long runTimeMs, int power) override {
1520
_log += "start(" +
1621
std::to_string(runTimeMs) + ", " +
1722
std::to_string(power) + ", " +
18-
std::to_string(currentTimeMs) +
23+
std::to_string(_env->time()) +
1924
")\n";
2025
}
2126

0 commit comments

Comments
 (0)