samedi 5 octobre 2019

How to write integration test for multi-language program?

I have created interprocess communication program where C# part is the writer to a shared memory (memory mapped file) and Visual C++ part is the reader. Both are console programs and produce executables. I want to write an integration test which verifies that the actual data written to shared memory by C# part is actually read by the C++ part.

The challenge here is how to write a test which supports both languages. Also the C++ part is not a dll so exporting functions is not an option. How can I go about writing this end to end test?

My code to read the shared memory is as follows:

string GetMessageFromSharedMem(const wchar_t*& memoryName, const wchar_t*& guardMutexName, int memAccess, DWORD offsetHigh, DWORD offsetLow, int sizeOfView) {
    HANDLE sharedMemHandle = INVALID_HANDLE_VALUE;
    LPVOID mappedViewOfFile = NULL;
    if (!(SharedMemManager::InitSharedMemHandle(sharedMemHandle, 
                                              memoryName,
                                              memAccess,
                                              false) == SharedMemManager::SHARED_MEM_HANDLE_OPENED 
            &&
          SharedMemManager::InitFileViewHandle(mappedViewOfFile, 
                                             sharedMemHandle, 
                                             memAccess,
                                             offsetHigh,
                                             offsetLow,
                                             sizeOfView) == SharedMemManager::FILE_VIEW_HANDLE_OPENED)) {
        throw std::exception("Could not open shared memory");
    }

    HANDLE mutexHandle = INVALID_HANDLE_VALUE;
    UINT mutexAcquireResult = Synchronization::AcquireMutex(mutexHandle, guardMutexName, SYNCHRONIZE, false, INFINITE);
    if (mutexAcquireResult != Synchronization::MUTEX_ACQUIRED) {
        throw std::exception("Could not acquire mutex");
    }

    int messageLength = 0;
    if (mappedViewOfFile != NULL) {
        messageLength = ((int*)mappedViewOfFile)[0];
    }
    char* buffer = (char*)malloc(messageLength + 1);
    if (buffer == NULL) {
        throw std::exception("Could not allocate memory for message");
    }
    memcpy(buffer, (char*)mappedViewOfFile + sizeof(int), messageLength);
    Synchronization::FreeMutex(mutexHandle);

    buffer[messageLength] = '\0';
    string message = buffer;
    free(buffer);

    return message;
}

Aucun commentaire:

Enregistrer un commentaire