Skip to content

Instantly share code, notes, and snippets.

@asfernandes
Created March 26, 2011 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asfernandes/888629 to your computer and use it in GitHub Desktop.
Save asfernandes/888629 to your computer and use it in GitHub Desktop.
Firebird events test using boost.
#include "ibase.h"
#include <string>
#include <boost/thread.hpp>
#define BOOST_TEST_MODULE LegacyTests
#include <boost/test/included/unit_test.hpp>
using std::string;
using boost::thread;
static const string LOCATION = "/tmp/";
BOOST_AUTO_TEST_SUITE(LegacyTests)
class PostThread
{
public:
const static int COUNT = 5;
PostThread(FB_API_HANDLE aAttachment)
: attachment(aAttachment)
{
}
PostThread(const PostThread& o)
: attachment(o.attachment)
{
}
void operator ()()
{
const char cmdTra[] = "set transaction";
const char cmdBlock[] = "execute block as begin post_event 'EVENT1'; end";
ISC_STATUS_ARRAY status = {0};
FB_API_HANDLE transaction = 0;
isc_dsql_execute_immediate(status, &attachment, &transaction, strlen(cmdTra), cmdTra,
3, NULL);
BOOST_CHECK(status[1] == 0);
BOOST_REQUIRE(transaction);
for (int i = 0; i < COUNT; ++i)
{
isc_dsql_execute_immediate(status, &attachment, &transaction, strlen(cmdBlock), cmdBlock,
3, NULL);
BOOST_CHECK(status[1] == 0);
}
isc_commit_transaction(status, &transaction);
BOOST_CHECK(status[1] == 0);
BOOST_REQUIRE(transaction == 0);
}
FB_API_HANDLE attachment;
};
BOOST_AUTO_TEST_CASE(testEvents)
{
const string location = LOCATION + "events.fdb";
ISC_STATUS_ARRAY status = {0};
FB_API_HANDLE attachment = 0;
isc_create_database(status, 0, location.c_str(), &attachment, 0, NULL, 0);
BOOST_CHECK(status[1] == 0);
BOOST_REQUIRE(attachment);
unsigned char* eveBuffer;
unsigned char* eveResult;
unsigned eveLen = isc_event_block(&eveBuffer, &eveResult, 1, "EVENT1");
// Make isc_wait_for_event wait instead of return counters before no event was happened.
eveBuffer[eveLen - 4] = 1;
thread thd = thread(PostThread(attachment));
isc_wait_for_event(status, &attachment, eveLen, eveBuffer, eveResult);
BOOST_CHECK(status[1] == 0);
thd.join();
ISC_ULONG increment = 0;
isc_event_counts(&increment, eveLen, eveBuffer, eveResult);
BOOST_CHECK(increment == PostThread::COUNT);
isc_free((char*) eveBuffer);
isc_free((char*) eveResult);
isc_drop_database(status, &attachment);
BOOST_CHECK(status[1] == 0);
BOOST_REQUIRE(attachment == 0);
}
BOOST_AUTO_TEST_SUITE_END()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment