Google Mock, expect call between 2 given numbers - googletest

I'm using google mock as a framework for testing a real time system, I want to verify that the configuration to the sensors is being implemented correctly. To do that I am sending a new Frequency to said sensor and expecting my sensor data callback to be called x number of times.
Due to this I have to sleep the testing thread for a few seconds, so my callback can be called a few times.
Due to this the expected calls are not a set number, for example, if I expect my callback to be called once per second and I sleep the thread 5 seconds, it can actually be called between 4 and 6 times, otherwise the test would be too restrictive.
This is the problem, I haven't found a way to test if expect call is between 4 and 6, I tried the following:
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AnyNumber());
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtMost(6));
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtLeast(4));
And
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AnyNumber());
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtLeast(4));
EXPECT_CALL(*handler,Data_Mock(_,_)).Times(::testing::AtMost(6));

Try Between from https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#cardinalities-cardinalitylist. It is exactly for the purpose of asserting that given call be called between m and n times.

Related

Akka.net scheduler sends just first value and doesn't update it, How fix it

I can't find the answer to an interesting moment.
in akka.net I have the scheduler. It will work in actor which are sort out a number.
here a simple implementation
_statusScheduler = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(
TimeSpan.FromSeconds(_shedulerInterval),
TimeSpan.FromSeconds(_shedulerInterval),
_reporterActor,
new ProgressReport(requestId, _testedQuantity),
Self);
where
_shedulerInterval - 5-second interval,
_testedQuantity - quantity of tested number all time updated.
and after 5 seconds it is sent 0; always, not a changed number. And here is a question: is it possible to send updated quantity?
I can't send the message to the updating quantity from Recieve<> methods, because my actor is handled the counting message and it is counted the quantity all the time and updated it(when it finished it will receive next message). But all five seconds I should generate a report by a scheduler. Is it possible to fix it?
I think now I need to send all logic because it works fine, and the stone of my problem is scheduler behavior.
The issue you have here is that the message you pass into the scheduler, new ProgressReport(requestId, _testedQuantity), is what is going to be sent each time. Since you're passing in those integer values by value, the object is going to have the original values for those fields at the time you created the message and therefore the message will never update.
If you want to be able to change the content that is sent in the recurring scheduler, do this instead:
var self = Self; // need a closure here, since ActorContext won't be available
_statusScheduler = Context.System.Scheduler.Advanced.ScheduleRepeatedlyCancelable(interval, interval, () => {
_reporterActor.Tell(new ProgressReport(requestId, _testedQuantity), self);
});
This usage of the scheduler will generate a new message each time when it invokes the lambda function and thus you'll be able to include the updated integer values inside your object.

How to prioritize a task in round robin scheduling with fixed time slice in KEIL-RTX RTOS environment

I have a situation here in my code where all tasks are running with same priority based on round robin (with fixed time slice of 50ms) scheduling algorithm. Now I want to run one particular task say Task A, exactly within a time period of 10ms to update some communication db. Since,current scheduling is based on round robin with fixed time slice of 50ms due to that the Task A is not able to get called exactly in 10ms. I am not getting any solution to cope up with the current problem.
Please do provide your valuable suggestion & advice.
Thanks in advance,
Vijay Khaitan
Not exactly sure what you are asking here. If you do not want Task A to run longer than 10ms, and you know that you will return from your communication functions in less than that, you can take a time reading at the beginning of Task A, and call osThreadYield() from Task A after you hit 10ms (busy loop).
If you are somewhere in Task B, and need to call Task A in exactly 10ms, it becomes a bit more complicated, since you don't know what thread can preempt your Task B at that time. What you can try, is in Task B, keep a handle to Task A. Then when you are ready to wait 10ms, do the following:
osThreadId id;
id = osThreadGetId (); // id for the currently running thread
osThreadSetPriority(id, osPriorityRealtime); // Make sure we get back here quickly
osWait(10); // Wait 10ms
osThreadSetPriority(id, osPriorityNormal); // Go back to normal
// If you need to create Task A, do so here, otherwise you can
// use osSignalSet here and osSignalWait in Task A
You can also call directly create Task A, set its priority to osPriorityRealtime, yield from Task B, and have the first method in Task A be osWait(10). As soon as you return, set its priority back to normal.

Erlang. Registering processes assignment

I am currently reading the Programming Erlang Second Edition Writing Software for a concurrent world written by Joe Armstrong and I have the following assignment :
Write a function start(AnAtom, Fun) to register AnAtom as spawn(Fun). Make sure your program works correctly in the case when two parallel processes simultaneously evaluate start/2. In this case you must guarantee that one succeeds and the other fails.
I understand the first bit. I need to register the process of Fun to the AnAtom. However what does the second part want me to do?
If two processes call start/2 at the same time then one of them must fail? Why? Given that the AnAtom is different to any others (which will be done inside the body of start/2 why would I want to fail one of the processes?
From what I can understand so far we have:
a = spawn(process1).
b = spawn(process2).
a ! {self(), registerProcess} //which should call the start/2
b ! {self(), registerProcess} //which should call the start/2
What is the problem here? Two processes will evaluate start/2. Why fail one of them? I'm probably missing the logic here or what I understood so far is completely wrong. Can anybody explain this in easier terms so I can get my head around it?
I believe the exercise is asking you to think about what happens when two parallel process evaluate start/2 using the SAME atom as the first parameter. When start(a, MyFunction) completes, there should be a spawned function (running MyFunction) associated with the name (atom) a.... what happens if
start(cool, MyFun1) and
start(cool, MyFun2)
are both executed simultaneously? How do you guarantee that one succeeds and the other fails.... does this help?
EDIT: I think you are not understanding the register process part of the assignment. When you get done with start(name, MyFun), doing a whereis(name) from the repl should return the process identifier of the process that got created.
This is not about sending the process a message to give it a name, it is about registering the process your created under the name passed in as the first parameter to start/2

CWInterface returning no data

I try to collect some information about the current state of the CWInterface(connected bssid, available access points...) and send them periodic (every 5-10 sec) via udp to a server.
My Problem is that after some time (between 30 and 50 min in some tests with different collection/sending interval) the CWInterface stops returning data.
[CWInterface interface] returns nil
[CWInterface interfaceNames] returns a NSSet with 0 entries
[[CWInterface interface] scanForNetworksWithSSID:nil &error] also returns a NSSet with 0
entries
What am I doing wrong?
I'm totaly out of ideas...
OK as I already commented my own question I changed the framework from CoreWlan to the private Apple80211.framework.
This seams to work.
My Application now runs for about one and a quater hour and scanning every few seconds.
Two negative points about using Apple80211 are:
There is no public documentation about how to use it (I used the documentation from http://code.google.com/p/iphone-wireless/ which also works for Mac OS X)
The scans now last about 5 seconds which is prety long but hey it works...

Time-based testing in Elixir

Let's say I have an Elixir function that should do something once every 30 minutes... Or not more often than once every 30 seconds, no matter how often it is called. Is there any good way of testing this, without having the test case take hours?
It is difficult to give an answer without having a specific use case, however one fairly simple option is to have the timeout be configurable (perhaps as a function argument) and test that.
e.g.
defmodule MyTest do
use Exunit.Case
test "message sent every 100 milliseconds" do
pid = self
MyModule.report_count_every(100, pid)
assert_receive({:ok, 1}, 500)
assert_receive({:ok, 2}, 500)
end
end
This assumes that MyModule is a GenServer that maintains a counter which it broadcasts based on the first argument passed to report_count_every/2