/* * Test for internal_flush() for the case when: * - secure_write returns 0 (send failed) * - errno is set to EPIPE */ void test__internal_flush_failedSendEPIPE(void **state) { int result; /* Simulating that secure_write will fail, and set the errno to EPIPE */ expect_any(secure_write, port); expect_any(secure_write, ptr); expect_any(secure_write, len); static int errval = EPIPE; will_return_with_sideeffect(secure_write, 0, _set_errno, &errval); /* In that case, we expect ereport(COMERROR, ...) to be called */ expect_value(errstart, elevel, COMMERROR); expect_any(errstart, filename); expect_any(errstart, lineno); expect_any(errstart, funcname); expect_any(errstart, domain); will_return(errstart, false); PqSendPointer = TEST_NUM_BYTES; /* Call function under test */ result = internal_flush(); assert_int_equal(result,EOF); assert_int_equal(ClientConnectionLost, 1); assert_int_equal(InterruptPending, 1); }
/* * Test for internal_flush() for the case when: * - secure_write returns 0 (send failed) * - errno is set to EINTR */ void test__internal_flush_failedSendEINTR(void **state) { int result; /* * In the case secure_write gets interrupted, we loop around and * try the send again. * In this test we simulate that, and secure_write will be called twice. * * First call to secure_write: returns 0 and sets errno to EINTR. */ expect_any(secure_write, port); expect_any(secure_write, ptr); expect_any(secure_write, len); static int errval = EINTR; will_return_with_sideeffect(secure_write, 0, _set_errno, &errval); /* Second call to secure_write: returns TEST_NUM_BYTES, i.e. success */ expect_any(secure_write, port); expect_any(secure_write, ptr); expect_any(secure_write, len); will_return(secure_write, TEST_NUM_BYTES); PqSendPointer = TEST_NUM_BYTES; /* Call function under test */ result = internal_flush(); assert_int_equal(result,0); assert_int_equal(ClientConnectionLost, 0); assert_int_equal(InterruptPending, 0); }
/* * Test that the spill_file->file_info->wfile field is initialized to NULL * when creating a workfile throws an exception. */ void test__getSpillFile__Initialize_wfile_exception(void **state) { int alloc_size = 0; int file_no = 0; int branching_factor = 32; ExecWorkFile *ewfile = (ExecWorkFile *) palloc0(sizeof(ExecWorkFile)); workfile_set *work_set = (workfile_set *) palloc0(sizeof(workfile_set)); SpillSet *spill_set = (SpillSet *) palloc0(sizeof(SpillSet) + (branching_factor-1) * sizeof (SpillFile)); SpillFile *spill_file = &spill_set->spill_files[0]; spill_file->file_info = NULL; /* Make workfile_mgr_create_file throw an exception, using the side effect function */ expect_value(workfile_mgr_create_file, work_set, work_set); will_return_with_sideeffect(workfile_mgr_create_file, ewfile, &throw_exception_side_effect, NULL); PG_TRY(); { /* This function will throw an exception, and we'll catch it below */ getSpillFile(work_set, spill_set, file_no, &alloc_size); } PG_CATCH(); { assert_true(spill_file->file_info != NULL); assert_int_equal(spill_file->file_info->total_bytes, 0); assert_int_equal(spill_file->file_info->ntuples, 0); assert_int_equal(alloc_size, 0); /* * This is the main test: We must initialize this pointer to NULL, even * if an exception is thrown */ assert_true(spill_file->file_info->wfile == NULL); return; } PG_END_TRY(); /* We shouldn't get here, the getSpillFile should throw an exception */ assert_true(false); }