Add gtest cases for ISR
This commit is contained in:
68
wiringPi/gtest/test_isr_conflict_between_processes.cpp
Normal file
68
wiringPi/gtest/test_isr_conflict_between_processes.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <wiringPi.h>
|
||||
|
||||
#define TRIGGER_PIN 26 // BCM 12
|
||||
#define LISTENER_PIN 31 // BCM 1
|
||||
#define ANOTHER_LISTENER_PIN 28 // BCM 20
|
||||
|
||||
static void *remote_isr(void *) {
|
||||
std::system("./bin/run_isr");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests ISR setup conflict between two processes.
|
||||
*
|
||||
* Another program 'run_isr' starts first that occupies the pin 31 for an ISR.
|
||||
* This program should fail to start ISR on the same pin.
|
||||
*/
|
||||
class ISRConflict : public ::testing::Test {
|
||||
public:
|
||||
static void SetUpTestSuite() {
|
||||
wiringPiSetup();
|
||||
digitalWrite(TRIGGER_PIN, LOW);
|
||||
|
||||
pthread_t tid;
|
||||
pthread_create(&tid, NULL, remote_isr, NULL);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
static void TearDownTestSuite() {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
static uint32_t count;
|
||||
|
||||
static void TriggerCount() { ++count; }
|
||||
static void TriggerCount2(WPIWfiStatus, void *userdata) {
|
||||
++*(uint32_t *)userdata;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void SetUp() { count = 0; }
|
||||
|
||||
virtual void TearDown() {
|
||||
digitalWrite(TRIGGER_PIN, LOW);
|
||||
delay(10);
|
||||
}
|
||||
};
|
||||
|
||||
uint32_t ISRConflict::count = 0;
|
||||
|
||||
TEST_F(ISRConflict, TestConflictBetweenProcesses) {
|
||||
// The ISRs should fail to start because run_isr is listening on the
|
||||
// same port
|
||||
EXPECT_EQ(
|
||||
wiringPiISR(LISTENER_PIN, INT_EDGE_FALLING, ISRConflict::TriggerCount),
|
||||
-1);
|
||||
EXPECT_EQ(wiringPiISR2(LISTENER_PIN, INT_EDGE_FALLING,
|
||||
ISRConflict::TriggerCount2, 0, &ISRConflict::count),
|
||||
-1);
|
||||
|
||||
// to exit run_isr
|
||||
digitalWrite(TRIGGER_PIN, HIGH);
|
||||
}
|
||||
Reference in New Issue
Block a user