File: output_buffer_unittest.cc

package info (click to toggle)
lizardfs 3.12.0+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 8,064 kB
  • sloc: cpp: 91,899; sh: 9,341; python: 3,878; ansic: 3,109; pascal: 128; makefile: 57
file content (67 lines) | stat: -rw-r--r-- 1,930 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
   Copyright 2013-2015 Skytechnology sp. z o.o.

   This file is part of LizardFS.

   LizardFS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, version 3.

   LizardFS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
 */

#include "common/platform.h"
#include <fcntl.h>
#include <cstdlib>
#include <string>
#include <gtest/gtest.h>

#include "chunkserver/output_buffer.h"
#include "unittests/TemporaryDirectory.h"

TEST(OutputBufferTests, outputBuffersTest) {
	OutputBuffer outputBuffer(512*1024);

	int auxPipeFileDescriptors[2];

#if defined(__APPLE__)
	ASSERT_NE(pipe(auxPipeFileDescriptors), -1);
#else
	ASSERT_NE(pipe2(auxPipeFileDescriptors, O_NONBLOCK), -1);
#endif

#ifdef F_SETPIPE_SZ
	ASSERT_NE(fcntl(auxPipeFileDescriptors[1], F_SETPIPE_SZ, 512*1024), -1);
#endif


	const unsigned WRITE_SIZE = 10;
	unsigned VALUE = 17u;

	uint8_t buf[WRITE_SIZE];
	memset(buf, VALUE, WRITE_SIZE);
	ASSERT_EQ(outputBuffer.copyIntoBuffer(buf, WRITE_SIZE), WRITE_SIZE);

	while (true) {
		OutputBuffer::WriteStatus status = outputBuffer.writeOutToAFileDescriptor(auxPipeFileDescriptors[1]);
		ASSERT_NE(status, OutputBuffer::WRITE_ERROR);
		if (status == OutputBuffer::WRITE_DONE) {
			break;
		}
		sleep(1);
	}

	ASSERT_EQ(read(auxPipeFileDescriptors[0], buf, WRITE_SIZE), WRITE_SIZE) << "errno: " << errno;

	for (unsigned j = 0; j < WRITE_SIZE; ++j) {
		ASSERT_EQ(VALUE, buf[j]) << "Byte " << j << " in block doesn't match";
	}
	close(auxPipeFileDescriptors[0]);
	close(auxPipeFileDescriptors[1]);
}