DetectorGraph  2.0
testtimeoutpublisherservice.cpp
Go to the documentation of this file.
1 // Copyright 2017 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 namespace DetectorGraph
18 {
19 
20 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
21 const TimeOffset TestTimeoutPublisherService::kInvalidMaxOffset = (TimeOffset)-1;
22 const TimeoutPublisherHandle TestTimeoutPublisherService::kMetronomeId = TimeOffsetsContainer::max_size - 1;
23 #else
24 const TimeOffset TestTimeoutPublisherService::kInvalidMaxOffset = std::numeric_limits<TimeOffset>::max();
25 const TimeoutPublisherHandle TestTimeoutPublisherService::kMetronomeId = kInvalidTimeoutPublisherHandle;
26 #endif
27 
29 : TimeoutPublisherService(arGraph), mElapsedTime(0), mWallClockOffset(0), mMetronomeTimerPeriod(0)
30 {
31 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
32  for (unsigned i = 0; i < TimeOffsetsContainer::max_size; i++)
33  {
34  mTimerDeadlines.push_back(TimeOffsetPairType(i, kInvalidMaxOffset));
35  }
36 #endif
37 }
38 
39 void TestTimeoutPublisherService::SetTimeout(const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerId)
40 {
41 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
42  mTimerDeadlines[aTimerId] = TimeOffsetPairType(aTimerId, aMillisecondsFromNow + mElapsedTime);
43 #else
44  mTimerDeadlines[aTimerId] = aMillisecondsFromNow + mElapsedTime;
45 #endif
46 }
47 
49 {
50 /* not much */
51 }
52 
54 {
55 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
56  mTimerDeadlines[aTimerId] = TimeOffsetPairType(aTimerId, kInvalidMaxOffset);
57 #else
58  mTimerDeadlines[aTimerId] = kInvalidMaxOffset;
59 #endif
60 }
61 
63 {
64  mMetronomeTimerPeriod = aPeriodInMilliseconds;
65  SetTimeout(mMetronomeTimerPeriod, kMetronomeId);
66  Start(kMetronomeId);
67 }
68 
70 {
71  Cancel(kMetronomeId);
72 }
73 
75 {
76  mWallClockOffset = aWallClockOffset;
77 }
78 
80 {
81  TimeOffsetsIterator minIt = GetNextTimeout();
82  if (minIt != mTimerDeadlines.end())
83  {
84  mElapsedTime = minIt->second;
85 
86  if (minIt->first == kMetronomeId)
87  {
89  SetTimeout(mMetronomeTimerPeriod, kMetronomeId);
90  Start(kMetronomeId);
91  }
92  else
93  {
94  TimeoutExpired(minIt->first);
95  minIt->second = kInvalidMaxOffset;
96  }
97 
98  return true;
99  }
100 
101  return false;
102 }
103 
105 {
106  bool firedAtLeastOne = false;
107  TimeOffset finalDeadline = mElapsedTime + aFwdTime;
108 
109  // If we're forwarding any amount of time ahead
110  // flush input queue first.
111  if (aFwdTime > 0)
112  {
113  while (aGraphToEvaluate.HasDataPending())
114  {
115  aGraphToEvaluate.EvaluateGraph();
116  }
117  }
118 
119  TimeOffsetsIterator minIt = GetNextTimeout();
120 
121  while (minIt != mTimerDeadlines.end())
122  {
123  TimeOffset nextDeadline = minIt->second;
124  if (nextDeadline > finalDeadline)
125  {
126  break;
127  }
128 
129  mElapsedTime = nextDeadline;
130 
131  if (minIt->first == kMetronomeId)
132  {
133  MetronomeFired();
134  SetTimeout(mMetronomeTimerPeriod, kMetronomeId);
135  Start(kMetronomeId);
136  }
137  else
138  {
139  TimeoutExpired(minIt->first);
140  minIt->second = kInvalidMaxOffset;
141  }
142 
143  firedAtLeastOne = true;
144 
145  while (aGraphToEvaluate.HasDataPending())
146  {
147  aGraphToEvaluate.EvaluateGraph();
148 
149  // If we're already at the targed time, evaluate only once and quit
150  // The idea is that finalDeadline is the "moment of interest" for a
151  // test; exiting here gives the test an opportunity inspect all
152  // outputs created for that particular moment in time.
153  if (nextDeadline == finalDeadline)
154  {
155  break;
156  }
157  }
158 
159  minIt = GetNextTimeout();
160  }
161 
162  mElapsedTime = finalDeadline;
163 
164  return firedAtLeastOne;
165 }
166 
168 {
169  return mElapsedTime + mWallClockOffset;
170 }
171 
173 {
174  return mElapsedTime;
175 }
176 
177 TestTimeoutPublisherService::TimeOffsetsIterator TestTimeoutPublisherService::GetNextTimeout()
178 {
179  TimeOffset nextDeadline = kInvalidMaxOffset;
180  TimeOffsetsIterator minIt = mTimerDeadlines.end();
181  TimeOffsetsIterator it = mTimerDeadlines.begin();
182  while (it != mTimerDeadlines.end())
183  {
184  if (it->second < nextDeadline)
185  {
186  minIt = it;
187  nextDeadline = it->second;
188  }
189  ++it;
190  }
191 
192  return minIt;
193 }
194 
196 {
197  return mMetronomeTimerPeriod;
198 }
199 
200 }
virtual void SetTimeout(const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerId)
Should setup a timeout for the given handle.
Implements a graph of Topics & Detectors with Input/Output APIs.
Definition: graph.hpp:127
virtual TimeOffset GetTime() const
Should return the time offset to Epoch.
virtual void Start(const TimeoutPublisherHandle aTimerId)
Should start a timer for the given handle.
A service that provides Timer function to DetectorGraph Detectors.
virtual TimeOffset GetMonotonicTime() const
Should return monotonic time since some unspecified starting point.
void TimeoutExpired(const TimeoutPublisherHandle aTimerHandle)
Fires/Dispatches a TopicState that was pending on a timeout.
virtual void CancelMetronome()
Should stop the metronome.
bool HasDataPending()
Returns true if there is data pending evaluation.
Definition: graph.cpp:97
void MetronomeFired()
Update metronome counters and Fires/Dispatches TopicStates that was pending on scheduled period...
virtual void StartMetronome(const TimeOffset aPeriodInMilliseconds)
Should start the metronome (periodic timer) for the given period.
ErrorType EvaluateGraph()
Evaluate the whole graph.
Definition: graph.cpp:133
bool ForwardTimeAndEvaluate(TimeOffset aFwdTime, Graph &aGraphToEvaluate)
virtual void Cancel(const TimeoutPublisherHandle aTimerId)
Should cancel the timer the given handle.