RPM Package Manager, CVS Repository http://rpm5.org/cvs/ ____________________________________________________________________________
Server: rpm5.org Name: Jeff Johnson Root: /v/rpm/cvs Email: j...@rpm5.org Module: rpm Date: 04-Apr-2011 01:06:20 Branch: rpm-5_4 Handle: 2011040323061900 Added files: (Branch: rpm-5_4) rpm/tests/mongo cpptest.cpp gridfs.c replica_set.c Log: - mongo: stub-in the new tests. entirely no workie ... Summary: Revision Changes Path 1.1.2.2 +75 -0 rpm/tests/mongo/cpptest.cpp 1.1.2.2 +235 -0 rpm/tests/mongo/gridfs.c 1.1.2.2 +38 -0 rpm/tests/mongo/replica_set.c ____________________________________________________________________________ patch -p0 <<'@@ .' Index: rpm/tests/mongo/cpptest.cpp ============================================================================ $ cvs diff -u -r0 -r1.1.2.2 cpptest.cpp --- /dev/null 2011-04-04 01:05:46.000000000 +0200 +++ cpptest.cpp 2011-04-04 01:06:20.008842790 +0200 @@ -0,0 +1,75 @@ +#include "system.h" + +#include "test.h" +#include "mongo.h" +#include <iostream> +#include <cstring> +#include <cstdio> + +#include "debug.h" + +// this is just a simple test to make sure everything works when compiled with a c++ compiler + +using namespace std; + +int main(int argc, char *argv[]) +{ + mongo_connection conn[1]; + mongo_connection_options opts; + bson_buffer bb; + bson b; + + strncpy(opts.host, (argc > 1 ? argv[1] : TEST_SERVER), 255); + + opts.host[254] = '\0'; + opts.port = 27017; + + if (mongo_connect( conn , &opts )){ + cout << "failed to connect" << endl; + return 1; + } + + for(int i=0; i< 5; i++){ + bson_buffer_init( & bb ); + + bson_append_new_oid( &bb, "_id" ); + bson_append_double( &bb , "a" , 17 ); + bson_append_int( &bb , "b" , 17 ); + bson_append_string( &bb , "c" , "17" ); + + { + bson_buffer * sub = bson_append_start_object( &bb , "d" ); + bson_append_int( sub, "i", 71 ); + bson_append_finish_object(sub); + } + { + bson_buffer * arr = bson_append_start_array( &bb , "e" ); + bson_append_int( arr, "0", 71 ); + bson_append_string( arr, "1", "71" ); + bson_append_finish_object(arr); + } + + bson_from_buffer(&b, &bb); + bson_destroy(&b); + } + + struct test_exception {}; + + bool caught = false; + try{ + MONGO_TRY{ + MONGO_THROW(MONGO_EXCEPT_NETWORK); + }MONGO_CATCH{ + throw test_exception(); + } + }catch (test_exception& e){ + caught = true; + } + + ASSERT(caught); + + mongo_destroy( conn ); + + return 0; +} + @@ . patch -p0 <<'@@ .' Index: rpm/tests/mongo/gridfs.c ============================================================================ $ cvs diff -u -r0 -r1.1.2.2 gridfs.c --- /dev/null 2011-04-04 01:05:46.000000000 +0200 +++ gridfs.c 2011-04-04 01:06:20.238854703 +0200 @@ -0,0 +1,235 @@ +#include "system.h" + +#include "test.h" +#include "mongo.h" +#ifdef NOTYET +#include "md5.h" +#include "gridfs.h" +#endif + +#include "debug.h" + +#if !defined(TEST_SERVER) +#define TEST_SERVER "127.0.0.1" +#endif + +#define LARGE 3*1024*1024 +#define UPPER 2000*1024 +#define LOWER 1024*128 +#define DELTA 1024*128 + +void fill_buffer_randomly(char * data, int64_t length) +{ + int64_t i; + int random; + char * letters = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + int nletters = strlen(letters)+1; + + for (i = 0; i < length; i++) { + random = rand() % nletters; + *(data + i) = letters[random]; + } +} + +static void digest2hex(mongo_md5_byte_t digest[16], char hex_digest[33]){ + static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; + int i; + for (i=0; i<16; i++){ + hex_digest[2*i] = hex[(digest[i] & 0xf0) >> 4]; + hex_digest[2*i + 1] = hex[ digest[i] & 0x0f ]; + } + hex_digest[32] = '\0'; +} + +void test_gridfile(gridfs *gfs, char *data_before, int64_t length, char *filename, char *content_type) { + gridfile gfile[1]; + char data_after[LARGE]; + FILE * fd; + mongo_md5_state_t pms[1]; + mongo_md5_byte_t digest[16]; + char hex_digest[33]; + + gridfs_find_filename(gfs, filename, gfile); + ASSERT(gridfile_exists(gfile)); + + fd = fopen("output", "w+"); + gridfile_write_file(gfile, fd); + fseek(fd, 0, SEEK_SET); + ASSERT(fread(data_after, length, sizeof(char), fd)); + fclose(fd); + ASSERT( strncmp(data_before, data_after, length) == 0 ); + + gridfile_read( gfile, length, data_after ); + ASSERT( strncmp(data_before, data_after, length) == 0 ); + + ASSERT( strcmp( gridfile_get_filename( gfile ), filename ) == 0 ); + + ASSERT( gridfile_get_contentlength( gfile ) == length ); + + ASSERT( gridfile_get_chunksize( gfile ) == DEFAULT_CHUNK_SIZE ); + + ASSERT( strcmp( gridfile_get_contenttype( gfile ), content_type ) == 0) ; + + mongo_md5_init(pms); + mongo_md5_append(pms, (const mongo_md5_byte_t *)data_before, (int)length); + mongo_md5_finish(pms, digest); + digest2hex(digest, hex_digest); + ASSERT( strcmp( gridfile_get_md5( gfile ), hex_digest ) == 0 ); + + gridfile_destroy(gfile); + gridfs_remove_filename(gfs, filename); +} + +void test_basic(int argc, char * argv[]) { + mongo_connection conn[1]; + mongo_connection_options opts; + gridfs gfs[1]; + char data_before[UPPER]; + int64_t i; + FILE *fd; + + srand(time(NULL)); + + strncpy(opts.host, (argc > 1 ? argv[1] : TEST_SERVER), 255); + + opts.host[254] = '\0'; + opts.port = 27017; + + if (mongo_connect( conn , &opts )){ + printf("failed to connect\n"); + exit(1); + } + + gridfs_init(conn, "test", "fs", gfs); + + for (i = LOWER; i <= UPPER; i+=DELTA) { + fill_buffer_randomly(data_before, i); + + /* Input from buffer */ + gridfs_store_buffer(gfs, data_before, i, "input-buffer", "text/html"); + test_gridfile(gfs, data_before, i, "input-buffer", "text/html"); + + /* Input from file */ + fd = fopen("input-file", "w"); + fwrite(data_before, sizeof(char), i, fd); + fclose(fd); + gridfs_store_file(gfs, "input-file", "input-file", "text/html"); + test_gridfile(gfs, data_before, i, "input-file", "text/html"); + } + + gridfs_destroy(gfs); + mongo_destroy(conn); +} + +void test_streaming(int argc, char * argv[]) { + mongo_connection conn[1]; + mongo_connection_options opts; + gridfs gfs[1]; + gridfile gfile[1]; + char buf[LARGE]; + char small[LOWER]; + int n; + + srand(time(NULL)); + + strncpy(opts.host, (argc > 1 ? argv[1] : TEST_SERVER), 255); + + opts.host[254] = '\0'; + opts.port = 27017; + + if (mongo_connect( conn , &opts )){ + printf("failed to connect\n"); + exit(1); + } + + fill_buffer_randomly(small, (int64_t)LOWER); + fill_buffer_randomly(buf, (int64_t)LARGE); + + gridfs_init(conn, "test", "fs", gfs); + + gridfs_store_buffer(gfs, small, LOWER, "small", "text/html"); + test_gridfile(gfs, small, LOWER, "small", "text/html"); + gridfs_destroy(gfs); + + gridfs_init(conn, "test", "fs", gfs); + gridfile_writer_init(gfile, gfs, "large", "text/html"); + for(n=0; n < (LARGE / 1024); n++) { + gridfile_write_buffer(gfile, buf + (n * 1024), 1024); + } + gridfile_writer_done( gfile ); + test_gridfile(gfs, buf, LARGE, "large", "text/html"); + + gridfs_destroy(gfs); + mongo_destroy(conn); +} + +void test_large(int argc char * argv[]) { + mongo_connection conn[1]; + mongo_connection_options opts; + gridfs gfs[1]; + gridfile gfile[1]; + FILE *fd; + int i, n; + char buffer[LARGE]; + int64_t filesize = (int64_t)1024 * (int64_t)LARGE; + + srand(time(NULL)); + + strncpy(opts.host, (argc > 1 ? argv[1] : TEST_SERVER), 255); + + opts.host[254] = '\0'; + opts.port = 27017; + + if (mongo_connect( conn , &opts )){ + printf("failed to connect\n"); + exit(1); + } + + gridfs_init(conn, "test", "fs", gfs); + + /* Create a very large file */ + fill_buffer_randomly(buffer, (int64_t)LARGE); + fd = fopen("bigfile", "w"); + for(i=0; i<1024; i++) { + fwrite(buffer, 1, LARGE, fd); + } + fclose(fd); + + /* Now read the file into GridFS */ + gridfs_store_file(gfs, "bigfile", "bigfile", "text/html"); + + gridfs_find_filename(gfs, "bigfile", gfile); + + ASSERT( strcmp( gridfile_get_filename( gfile ), "bigfile" ) == 0 ); + ASSERT( gridfile_get_contentlength( gfile ) == filesize ); + + /* Read the file using the streaming interface */ + gridfile_writer_init( gfile, gfs, "bigfile-stream", "text/html"); + + fd = fopen("bigfile", "r"); + + while((n = fread(buffer, 1, 1024, fd)) != 0) { + gridfile_write_buffer(gfile, buffer, n); + } + gridfile_writer_done( gfile ); + + gridfs_find_filename(gfs, "bigfile-stream", gfile); + + ASSERT( strcmp( gridfile_get_filename( gfile ), "bigfile-stream" ) == 0 ); + ASSERT( gridfile_get_contentlength( gfile ) == filesize ); + + gridfs_destroy(gfs); + mongo_destroy(conn); +} + +int main(int argc, char *argv[]) +{ + test_basic(argc, argv); + test_streaming(argc, argv); + + /* Normally not necessary to run test_large(), as it + * deals with very large (3GB) files and is therefore slow. + * test_large(argc, argv); + */ + return 0; +} @@ . patch -p0 <<'@@ .' Index: rpm/tests/mongo/replica_set.c ============================================================================ $ cvs diff -u -r0 -r1.1.2.2 replica_set.c --- /dev/null 2011-04-04 01:05:46.000000000 +0200 +++ replica_set.c 2011-04-04 01:06:20.368860040 +0200 @@ -0,0 +1,38 @@ +/* test.c */ + +#include "system.h" + +#include "test.h" +#include "mongo.h" + +#include "debug.h" + + +int main(int argc, char *argv[]) +{ + mongo_connection conn[1]; + +#ifdef NOTYET + INIT_SOCKETS_FOR_WINDOWS; +#else + strncpy(conn.host, (argc > 1 ? argv[1] : TEST_SERVER), 255); + conn.host[254] = '\0'; + conn.port = 27017; +#endif + + mongo_replset_init_conn( conn ); + mongo_replset_add_seed( conn, TEST_SERVER, 30000 ); + mongo_replset_add_seed( conn, TEST_SERVER, 30001 ); + mongo_host_port* p = conn->seeds; + + while( p != NULL ) { + p = p->next; + } + + if( mongo_replset_connect( conn ) ) { + printf("Failed to connect."); + exit(1); + } + + return 0; +} @@ . ______________________________________________________________________ RPM Package Manager http://rpm5.org CVS Sources Repository rpm-cvs@rpm5.org