Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
TestMCTransport.cpp File Reference
#include <stdio.h>
#include "nsIServiceManager.h"
#include "nsIEventQueueService.h"
#include "nsIOutputStream.h"
#include "nsIStreamListener.h"
#include "nsITransport.h"
#include "nsIInputStream.h"
#include "nsCOMPtr.h"
#include "plstr.h"
#include "prprf.h"
#include "nsICacheService.h"
#include "nsICacheSession.h"
#include "nsICacheEntryDescriptor.h"
#include "nsNetCID.h"

Go to the source code of this file.


class  TestListener


 This test program exercises the memory cache's nsITransport implementation.
 NS_IMPL_ISUPPORTS2 (TestListener, nsIStreamListener, nsIRequestObserver) TestListener
nsresult TestMCTransport (const char *filename)
int main (int argc, char **argv)
 The Xalan testcases app.


static nsICacheSessionsession = nsnull
static nsICacheEntryDescriptordesc = nsnull
static nsIEventQueuegEventQ = nsnull

Function Documentation

int main ( int  argc,
char **  argv 

The Xalan testcases app.

Definition at line 220 of file TestMCTransport.cpp.

    nsresult rv;

    if (argc < 2) {
        printf("usage: %s filename\n", argv[0]);
        return -1;

    nsCOMPtr<nsIEventQueueService> eqs = 
             do_GetService(kEventQueueServiceCID, &rv);
    if (NS_FAILED(rv)) {
        printf("failed to create event queue service: rv=%x\n", rv);
        return -1;

    rv = eqs->CreateMonitoredThreadEventQueue();
    if (NS_FAILED(rv)) {
        printf("failed to create monitored event queue: rv=%x\n", rv);
        return -1;

    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
    if (NS_FAILED(rv)) {
        printf("failed to get thread event queue: %x\n", rv);
        return -1;

    rv = TestMCTransport(argv[1]);
    printf("TestMCTransport returned %x\n", rv);


    return 0;

Here is the call graph for this function:

static NS_DEFINE_CID ( kCacheServiceCID  ,
) [static]

This test program exercises the memory cache's nsITransport implementation.

This test program loads a file into the memory cache (using OpenOutputStream), and then reads the file back out (using AsyncRead). The data read from the memory cache is written to a new file (with .out added as a suffix to the file name).

Definition at line 89 of file TestMCTransport.cpp.

    : mFilename(filename)
    , mFile(nsnull)
nsresult TestMCTransport ( const char *  filename)

Definition at line 151 of file TestMCTransport.cpp.

    nsresult rv = NS_OK;
    nsCOMPtr<nsITransport> transport;

    transport = do_CreateInstance(
            ";1", &rv);
    if (NS_FAILED(rv))
        return rv;
    nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
    if (NS_FAILED(rv)) return rv;

    rv = serv->CreateSession("TestMCTransport",
                             nsICache::STORE_IN_MEMORY, PR_TRUE,
    if (NS_FAILED(rv)) return rv;

    rv = session->OpenCacheEntry(nsDependentCString(filename),
    if (NS_FAILED(rv)) return rv;

    rv = desc->MarkValid();
    if (NS_FAILED(rv)) return rv;

    rv = desc->GetTransport(getter_AddRefs(transport));
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIOutputStream> os;
    rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
    if (NS_FAILED(rv)) return rv;

    char *out = PR_smprintf("%s.out", filename);
    nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
    if (!listener)
        return NS_ERROR_OUT_OF_MEMORY;

    nsCOMPtr<nsIRequest> req;
    rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
    if (NS_FAILED(rv)) return rv;

    FILE *file = fopen(filename, "r");
    if (!file)
        return NS_ERROR_FILE_NOT_FOUND;

    char buf[256];
    PRUint32 count, total=0;

    while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
        printf("writing %u bytes\n", count);
        total += count;
        rv = os->Write(buf, count, &count);
        if (NS_FAILED(rv)) return rv;

        // process an event
        PLEvent *event = nsnull;
        if (event) gEventQ->HandleEvent(event);

    printf("wrote %u bytes\n", total);

    return rv;

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

Definition at line 59 of file TestMCTransport.cpp.

Definition at line 72 of file TestMCTransport.cpp.

Definition at line 58 of file TestMCTransport.cpp.