void
test_mongo_sync_gridfs_chunked_find (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  bson *query;

  query = bson_build (BSON_TYPE_STRING, "filename", "bogus-fn", -1,
                      BSON_TYPE_NONE);
  bson_finish (query);

  ok (mongo_sync_gridfs_chunked_find (NULL, query) == NULL,
      "mongo_sync_gridfs_chunked_find() fails with a NULL GridFS");

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  ok (mongo_sync_gridfs_chunked_find (gfs, NULL) == NULL,
      "mongo_sync_gridfs_chunked_find() fails with a NULL query");

  ok (mongo_sync_gridfs_chunked_find (gfs, query) == NULL,
      "mongo_sync_gridfs_chunked_find() fails when the file is not found");

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (query);
}
void
test_mongo_sync_cmd_kill_cursors_net (void)
{
  mongo_packet *p;
  mongo_sync_connection *conn;
  bson *b;
  gint i;
  mongo_reply_packet_header rh;
  gint64 cid;

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (conn, TRUE);

  b = bson_new ();
  for (i = 0; i < 40; i++)
    {
      bson_reset (b);
      bson_append_string (b, "test-name", __FILE__, -1);
      bson_append_int32 (b, "seq", i);
      bson_finish (b);

      mongo_sync_cmd_insert (conn, config.ns, b, NULL);
    }
  bson_free (b);

  b = bson_new ();
  bson_append_string (b, "test-name", __FILE__, -1);
  bson_finish (b);

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_kill_cursors() works");

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  bson_free (b);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_cmd_kill_cursors() automatically reconnects");

  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_kill_cursors_net_secondary ();

  end_network_tests ();
}
void
test_mongo_sync_gridfs_get_set_chunk_size (void)
{
  mongo_sync_gridfs *gfs;

  ok (mongo_sync_gridfs_get_chunk_size (NULL) == -1,
      "mongo_sync_gridfs_get_chunk_size() fails with a NULL gfs");
  ok (mongo_sync_gridfs_set_chunk_size (NULL, 16 * 1024) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails with a NULL gfs");

  begin_network_tests (3);

  gfs = mongo_sync_gridfs_new (mongo_sync_connect (config.primary_host,
                                                   config.primary_port,
                                                   FALSE),
                               config.gfs_prefix);

  ok (mongo_sync_gridfs_set_chunk_size (gfs, -1) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails if the size is invalid");
  ok (mongo_sync_gridfs_set_chunk_size (gfs, 12345),
      "mongo_sync_gridfs_set_chunk_size() works");
  cmp_ok (mongo_sync_gridfs_get_chunk_size (gfs), "==", 12345,
          "mongo_sync_gridfs_get_chunk_size() works");

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
void
test_mongo_connection_set_timeout (void)
{
  mongo_connection c, *conn;

  c.fd = -1;

  ok (mongo_connection_set_timeout (NULL, 100) == FALSE,
      "mongo_connection_set_timeout() should fail with a NULL connection");
  ok (mongo_connection_set_timeout (&c, -1) == FALSE,
      "mongo_connection_set_timeout() should fail with a negative timeout");
  ok (mongo_connection_set_timeout (&c, 100) == FALSE,
      "mongo_connection_set_timeout() should fail with an invalid FD");

  begin_network_tests (0);

  conn = mongo_connect (config.primary_host, config.primary_port);

  /* No verification here, as some systems may or may not support
     this, thus, failing in a test is not fatal. */
  mongo_connection_set_timeout (conn, 100);

  mongo_disconnect (conn);

  end_network_tests ();
}
void
test_mongo_sync_cmd_user_add_net (void)
{
  mongo_sync_connection *c;

  begin_network_tests (3);

  c = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ok (mongo_sync_cmd_user_add (c, config.db, "test", "s3kr1+") == TRUE,
      "mongo_sync_cmd_user_add() works");

  shutdown (c->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_user_add (c, config.db, "test", "s3kr1+") == TRUE,
      "mongo_sync_cmd_user_add() automatically reconnects");

  mongo_sync_disconnect (c);

  test_mongo_sync_cmd_user_add_net_secondary ();

  end_network_tests ();
}
void
test_mongo_sync_gridfs_stream_seek (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;

  mongo_util_oid_init (0);

  ok (mongo_sync_gridfs_stream_seek (NULL, 0, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with a NULL stream");

  begin_network_tests (8);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);

  ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with a write stream");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_SET and a negative "
      "position");

  ok (mongo_sync_gridfs_stream_seek (stream, 10, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_SET and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_CUR) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_CUR and a position "
      "before the start");

  ok (mongo_sync_gridfs_stream_seek (stream, 10, SEEK_CUR) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_CUR and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_END) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_END and a position "
      "past EOF");

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_END) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with SEEK_END and a position "
      "before the start");

  ok (mongo_sync_gridfs_stream_seek (stream, 0, 42) == FALSE,
      "mongo_sync_gridfs_stream_seek() fails with an invalid whence");

  mongo_sync_gridfs_stream_close (stream);
  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
void
test_mongo_sync_cmd_is_master_net (void)
{
  mongo_sync_connection *conn;

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  ok (mongo_sync_cmd_is_master (conn) == TRUE,
      "mongo_sync_cmd_is_master() works");
  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_is_master_net_secondary ();

  end_network_tests ();
}
Beispiel #8
0
void
test_mongo_sync_cmd_create_net (void)
{
  mongo_sync_connection *conn;
  gchar *cc;

  begin_network_tests (5);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);

  cc = g_strconcat (config.coll, ".capped", NULL);

  mongo_sync_cmd_drop (conn, config.db, config.coll);
  mongo_sync_cmd_drop (conn, config.db, cc);

  ok (mongo_sync_cmd_create (conn, config.db, config.coll,
			     MONGO_COLLECTION_DEFAULTS) == TRUE,
      "mongo_sync_cmd_create() can create normal collections");
  mongo_sync_cmd_drop (conn, config.db, config.coll);

  ok (mongo_sync_cmd_create (conn, config.db, config.coll,
			     MONGO_COLLECTION_SIZED,
			     (gint64) 64 * 1024 * 10) == TRUE,
      "mongo_sync_cmd_create() can create pre-allocated collections");

  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED, (gint64) -1) == FALSE,
      "mongo_sync_cmd_create() fails when trying to create a capped "
      "collection with an invalid size");
  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED_MAX,
			     (gint64) (64 * 1024 * 10), (gint64) -1) == FALSE,
      "mongo_sync_cmd_create() fails when trying to create a capped "
      "collection with invalid max.");
  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED_MAX |
			     MONGO_COLLECTION_AUTO_INDEX_ID,
			     (gint64)(64 * 1024 * 10), (gint64) 10) == TRUE,
      "mongo_sync_cmd_create() can create capped collections");

  mongo_sync_cmd_drop (conn, config.db, cc);

  g_free (cc);
  mongo_sync_disconnect (conn);

  end_network_tests ();
}
Beispiel #9
0
void
test_mongo_sync_gridfs_new (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  gchar *f, *c;

  conn = test_make_fake_sync_conn (4, TRUE);

  ok (mongo_sync_gridfs_new (NULL, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL connection");

  ok (mongo_sync_gridfs_new (conn, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus connection");

  ok (mongo_sync_gridfs_new (conn, NULL) == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL ns prefix");

  ok (mongo_sync_gridfs_new (conn, "bogus") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus ns prefix");

  mongo_sync_disconnect (conn);

  begin_network_tests (4);

  f = g_strconcat (config.gfs_prefix, ".files", NULL);
  c = g_strconcat (config.gfs_prefix, ".chunks", NULL);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);

  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  ok (gfs != NULL,
      "mongo_sync_gridfs_new() works");
  is (gfs->ns.prefix, config.gfs_prefix,
      "The namespace prefix is as specified");
  is (gfs->ns.files, f,
      "The files namespace is correct");
  is (gfs->ns.chunks, c,
      "The chunks namespace is correct");
  mongo_sync_gridfs_free (gfs, FALSE);

  mongo_sync_disconnect (conn);

  g_free (f);
  g_free (c);
  end_network_tests ();
}
Beispiel #10
0
void
test_mongo_sync_connect (void)
{
  mongo_sync_connection *c;

  ok (mongo_sync_connect (NULL, 27017, FALSE) == NULL,
      "mongo_sync_connect() fails with a NULL host");

  begin_network_tests (1);

  ok ((c = mongo_sync_connect (config.primary_host,
			       config.primary_port, FALSE)) != NULL,
      "mongo_sync_connect() works");
  mongo_sync_disconnect (c);

  end_network_tests ();
}
Beispiel #11
0
void
test_mongo_packet_recv (void)
{
  mongo_connection c, *conn;
  mongo_packet *p;
  bson *b;

  c.fd = -1;

  ok (mongo_packet_recv (NULL) == NULL,
      "mongo_packet_recv() fails with a NULL connection");
  ok (errno == ENOTCONN,
      "mongo_packet_recv() sets errno to ENOTCONN if connection is NULL");

  ok (mongo_packet_recv (&c) == NULL,
      "mongo_packet_recv() fails if the FD is less than zero");
  ok (errno == EBADF,
      "mongo_packet_recv() sets errno to EBADF is the FD is bad");

  begin_network_tests (2);

  b = bson_new ();
  bson_append_int32 (b, "getnonce", 1);
  bson_finish (b);

  p = mongo_wire_cmd_custom (42, config.db, 0, b);
  bson_free (b);

  conn = mongo_connect (config.primary_host, config.primary_port);
  mongo_packet_send (conn, p);
  mongo_wire_packet_free (p);

  ok ((p = mongo_packet_recv (conn)) != NULL,
      "mongo_packet_recv() works");
  mongo_wire_packet_free (p);

  close (conn->fd);
  sleep (3);

  ok (mongo_packet_recv (conn) == NULL,
      "mongo_packet_recv() fails on a closed socket");

  mongo_disconnect (conn);

  end_network_tests ();
}
void
test_mongo_sync_gridfs_stream_write (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  guint8 buffer[4096];

  mongo_util_oid_init (0);

  meta = bson_build (BSON_TYPE_STRING, "my-id", "sync_gridfs_stream_write", -1,
                     BSON_TYPE_NONE);
  bson_finish (meta);

  ok (mongo_sync_gridfs_stream_write (NULL, buffer, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a NULL connection");

  begin_network_tests (4);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);

  ok (mongo_sync_gridfs_stream_write (stream, NULL, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a NULL buffer");
  ok (mongo_sync_gridfs_stream_write (stream, buffer, 0) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with 0 size");
  ok (mongo_sync_gridfs_stream_write (stream, buffer, sizeof (buffer)) == TRUE,
      "mongo_sync_gridfs_stream_write() works");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;
  ok (mongo_sync_gridfs_stream_write (stream, buffer, sizeof (buffer)) == FALSE,
      "mongo_sync_gridfs_stream_write() should fail with a read stream");

  mongo_sync_gridfs_stream_close (stream);
  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (meta);
}
void
test_mongo_sync_gridfs_stream_new (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;

  mongo_util_oid_init (0);

  meta = bson_build (BSON_TYPE_STRING, "my-id", "sync_gridfs_stream_new", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  ok (mongo_sync_gridfs_stream_new (NULL, meta) == FALSE,
      "mongo_sync_gridfs_stream_new() should fail with a NULL connection");

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_new() works with NULL metadata");
  mongo_sync_gridfs_stream_close (stream);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_new() works with metadata");
  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (meta);
}
void
test_mongo_connection_get_requestid (void)
{
  mongo_connection c, *conn;
  mongo_packet *p;
  bson *b;
  gint reqid;

  c.request_id = 42;

  ok (mongo_connection_get_requestid (NULL) == -1,
      "mongo_connection_get_requestid() fails with a NULL connection");
  ok (mongo_connection_get_requestid (&c) == 42,
      "mongo_connection_get_requestid() works");

  begin_network_tests (2);

  b = bson_new ();
  bson_append_int32 (b, "getnonce", 1);
  bson_finish (b);

  p = mongo_wire_cmd_custom (42, config.db, 0, b);
  bson_free (b);

  conn = mongo_connect (config.primary_host, config.primary_port);
  cmp_ok ((reqid = mongo_connection_get_requestid (conn)), "==", 0,
	  "Initial request id is 0");
  mongo_packet_send (conn, p);
  mongo_wire_packet_free (p);

  cmp_ok (reqid, "<", mongo_connection_get_requestid (conn),
	  "Old request ID is smaller than the new one");

  mongo_disconnect (conn);

  end_network_tests ();
}
void
test_mongo_sync_gridfs_stream_read (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  guint8 buffer[4096];

  mongo_util_oid_init (0);

  ok (mongo_sync_gridfs_stream_read (NULL, buffer, sizeof (buffer)) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a NULL connection");

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);

  ok (mongo_sync_gridfs_stream_read (stream, buffer, sizeof (buffer)) == -1,
      "mongo-sync_gridfs_stream_read() should fail when the stream is "
      "write-only");

  stream->file.type = LMC_GRIDFS_FILE_STREAM_READER;

  ok (mongo_sync_gridfs_stream_read (stream, NULL, sizeof (buffer)) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a NULL buffer");
  ok (mongo_sync_gridfs_stream_read (stream, buffer, 0) == -1,
      "mongo_sync_gridfs_stream_read() should fail with a 0 size");

  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
Beispiel #16
0
void
test_mongo_sync_cmd_custom_net (void)
{
  mongo_sync_connection *conn;
  bson *cmd;
  mongo_packet *p;

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_cmd_is_master (conn);
  mongo_sync_conn_set_auto_reconnect (conn, TRUE);

  cmd = bson_build (BSON_TYPE_INT32, "getnonce", 1,
		    BSON_TYPE_NONE);
  bson_finish (cmd);

  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  ok (p != NULL,
      "mongo_sync_cmd_custom() works");
  mongo_wire_packet_free (p);

  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);

  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  ok (p != NULL,
      "mongo_sync_cmd_custom() automatically reconnects");
  mongo_wire_packet_free (p);

  bson_free (cmd);
  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_custom_net_secondary ();

  end_network_tests ();
}
void
test_mongo_sync_cmd_insert (void)
{
  mongo_sync_connection *c;
  bson *b1, *b2;

  c = test_make_fake_sync_conn (-1, FALSE);
  b1 = test_bson_generate_full ();
  b2 = test_bson_generate_full ();

  ok (mongo_sync_cmd_insert (NULL, "test.ns", b1, b2, NULL) == FALSE,
      "mongo_sync_cmd_insert() fails with a NULL connection");
  ok (mongo_sync_cmd_insert (c, NULL, b1, b2, NULL) == FALSE,
      "mongo_sync_cmd_insert() fails with a NULL namespace");
  ok (mongo_sync_cmd_insert (c, "test.ns", NULL) == FALSE,
      "mongo_sync_cmd_insert() fails with no documents to insert");
  ok (mongo_sync_cmd_insert (c, "test.ns", b1, b2, NULL) == FALSE,
      "mongo_sync_cmd_insert() fails with a bogus FD");

  mongo_sync_disconnect (c);
  bson_free (b1);
  bson_free (b2);

  begin_network_tests (4);

  b1 = bson_new ();
  bson_append_string (b1, "sync_cmd_insert", "works", -1);
  bson_finish (b1);
  b2 = bson_new ();
  bson_append_int32 (b2, "int32", 1984);
  bson_finish (b2);

  c = mongo_sync_connect (config.primary_host, config.primary_port,
                          TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ok (mongo_sync_cmd_insert (c, config.ns, b1, b2, NULL) == TRUE,
      "mongo_sync_cmd_insert() works");

  shutdown (c->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_insert (c, config.ns, b1, b2, NULL) == TRUE,
      "mongo_sync_cmd_insert() automatically reconnects");

  mongo_sync_disconnect (c);

  /*
   * Tests involving a secondary
   */
  skip (!config.secondary_host, 2, "Secondary host not set up");

  c = mongo_sync_connect (config.secondary_host, config.secondary_port,
                          TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ok (c && mongo_sync_cmd_is_master (c) == FALSE,
      "Connected to a secondary");

  ok (mongo_sync_cmd_insert (c, config.ns, b1, b2, NULL) == TRUE,
      "mongo_sync_cmd_insert() automatically reconnects to master");
  mongo_sync_disconnect (c);

  endskip;

  bson_free (b1);
  bson_free (b2);

  end_network_tests ();
}
void
test_mongo_sync_gridfs_chunked_file_new_from_buffer (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  bson *metadata;
  guint8 *buffer;
  mongo_sync_gridfs_chunked_file *gfile;

  buffer = g_malloc (BUFFER_SIZE);
  memset (buffer, 'a', BUFFER_SIZE);

  conn = test_make_fake_sync_conn (4, TRUE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  metadata = bson_build (BSON_TYPE_STRING, "filename",
                         "gridfs_file_new_from_buffer", -1,
                         BSON_TYPE_NONE);
  bson_finish (metadata);

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (NULL, metadata,
                                                      buffer, BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with a NULL GridFS");

  mongo_sync_gridfs_free (gfs, TRUE);

  begin_network_tests (5);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                      NULL, BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with NULL data");

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                      buffer, 0) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with an invalid data size");

  ok (mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata, buffer,
                                                      BUFFER_SIZE) == FALSE,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() fails with uninitialized OID");

  mongo_util_oid_init (0);

  gfile = mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, metadata,
                                                          buffer, BUFFER_SIZE);
  ok (gfile != NULL,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() works with metadata");
  mongo_sync_gridfs_chunked_file_free (gfile);

  gfile = mongo_sync_gridfs_chunked_file_new_from_buffer (gfs, NULL,
                                                          buffer, BUFFER_SIZE);
  ok (gfile != NULL,
      "mongo_sync_gridfs_chunked_file_new_from_buffer() works without metadata");
  mongo_sync_gridfs_chunked_file_free (gfile);

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();

  bson_free (metadata);
  g_free (buffer);
}
Beispiel #19
0
void
test_mongo_sync_reconnect (void)
{
  mongo_sync_connection *conn, *o;
  GList *l;

  ok (mongo_sync_reconnect (NULL, FALSE) == NULL,
      "mongo_sync_reconnect() fails with a NULL connection");
  cmp_ok (errno, "==", ENOTCONN,
	  "errno is ENOTCONN");

  conn = test_make_fake_sync_conn (-1, FALSE);
  ok (mongo_sync_reconnect (conn, FALSE) == NULL,
      "mongo_sync_reconnect() fails with a bogus FD");
  cmp_ok (errno, "==", EHOSTUNREACH,
	  "errno is EHOSTUNREACH");

  mongo_sync_disconnect (conn);

  begin_network_tests (15);

  /* Connect & reconnect to master */
  o = conn = mongo_sync_connect (config.primary_host,
				 config.primary_port, TRUE);
  ok ((conn = mongo_sync_reconnect (conn, TRUE)) != NULL,
      "mongo_sync_reconnect() works when reconnecting to self");
  ok (o == conn,
      "Reconnect to an existing master results in the same object");
  mongo_sync_disconnect (conn);

  /* Connect to master, kill FD, reconnect */
  conn = mongo_sync_connect (config.primary_host,
			     config.primary_port, TRUE);
  mongo_sync_cmd_is_master (conn);

  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);

  ok ((conn = mongo_sync_reconnect (conn, TRUE)) != NULL,
       "mongo_sync_reconnect() succeed when the connection drops");
  mongo_sync_disconnect (conn);

  /* Connect, kill, reconnect; w/o knowing other hosts */
  o = conn = mongo_sync_connect (config.primary_host,
				 config.primary_port, TRUE);
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);
  l = conn->rs.hosts;
  while (l)
    {
      g_free (l->data);
      l = g_list_delete_link (l, l);
    }
  conn->rs.hosts = NULL;

  conn = mongo_sync_reconnect (conn, FALSE);

  ok (conn != o && conn == NULL,
      "mongo_sync_reconnect() fails if it can't reconnect anywhere");
  mongo_sync_disconnect (o);

  /* Gracefully ignore unparsable hosts during reconnect */
  o = conn = mongo_sync_connect (config.primary_host,
				 config.primary_port, TRUE);
  mongo_sync_cmd_is_master (conn);
  conn->rs.hosts = g_list_prepend (conn->rs.hosts,
				   g_strdup ("invalid:-42"));
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);
  conn = mongo_sync_reconnect (conn, TRUE);

  ok (conn == o,
      "mongo_sync_reconnect() gracefully ignores unparsable hosts "
      "during reconnect");
  mongo_sync_disconnect (conn);

  /* Ignore unreachable hosts during reconnect */
  o = conn = mongo_sync_connect (config.primary_host,
				 config.primary_port, TRUE);
  mongo_sync_cmd_is_master (conn);
  conn->rs.hosts = g_list_prepend (conn->rs.hosts,
				   g_strdup ("example.com:27017"));
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);
  conn = mongo_sync_reconnect (conn, TRUE);

  ok (conn == o,
      "mongo_sync_reconnect() gracefully ignores unparsable hosts "
      "during reconnect");
  mongo_sync_disconnect (conn);

  /*
   * Tests involving a secondary
   */

  skip (!config.secondary_host, 9,
	"Secondary host not set up");

  /* Connect to secondary & reconnect to master */
  o = conn = mongo_sync_connect (config.secondary_host,
				 config.secondary_port, TRUE);
  ok (conn != NULL, "Connecting to secondary");
  ok (mongo_sync_cmd_is_master (conn) == FALSE,
      "Connected to a secondary");
  ok ((conn = mongo_sync_reconnect (conn, TRUE)) != NULL,
      "Reconnecting from slave to master succeeds");
  ok (conn == o, "Connection object updated in-place");
  ok (mongo_sync_cmd_is_master (conn),
      "Correctly reconnected to master");
  mongo_sync_disconnect (conn);

  /* Connect to secondary & reconnect to self */
  o = conn = mongo_sync_connect (config.secondary_host,
				 config.secondary_port, TRUE);
  ok (conn != NULL, "Connecting to secondary");
  ok ((conn = mongo_sync_reconnect (conn, FALSE)) != NULL,
      "Reconnecting from slave to self succeeds");
  ok (conn == o, "Connection object updated in-place");
  ok (mongo_sync_cmd_is_master (conn) == FALSE,
      "Correctly reconnected to self");
  mongo_sync_disconnect (conn);

  endskip;

  end_network_tests ();
}