Skip to content

Commit c1354b5

Browse files
committed
HDFS-14478. Add libhdfs APIs for openFile (apache#4166)
Contributed by Sahil Takiar
1 parent 2112ef6 commit c1354b5

File tree

9 files changed

+807
-11
lines changed

9 files changed

+807
-11
lines changed

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/test_libhdfs_ops.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,68 @@ int main(int argc, char **argv) {
454454
hdfsCloseFile(lfs, localFile);
455455
}
456456

457+
458+
{
459+
// HDFS Open File Builder tests
460+
461+
exists = hdfsExists(fs, readPath);
462+
463+
if (exists) {
464+
fprintf(stderr, "Failed to validate existence of %s\n", readPath);
465+
shutdown_and_exit(cl, -1);
466+
}
467+
468+
hdfsOpenFileBuilder *builder;
469+
builder = hdfsOpenFileBuilderAlloc(fs, readPath);
470+
hdfsOpenFileBuilderOpt(builder, "hello", "world");
471+
472+
hdfsOpenFileFuture *future;
473+
future = hdfsOpenFileBuilderBuild(builder);
474+
475+
readFile = hdfsOpenFileFutureGet(future);
476+
if (!hdfsOpenFileFutureCancel(future, 0)) {
477+
fprintf(stderr, "Cancel on a completed Future should return false");
478+
shutdown_and_exit(cl, -1);
479+
}
480+
hdfsOpenFileFutureFree(future);
481+
482+
memset(buffer, 0, sizeof(buffer));
483+
num_read_bytes = hdfsRead(fs, readFile, (void *) buffer,
484+
sizeof(buffer));
485+
if (strncmp(fileContents, buffer, strlen(fileContents)) != 0) {
486+
fprintf(stderr,
487+
"Failed to read. Expected %s but got %s (%d bytes)\n",
488+
fileContents, buffer, num_read_bytes);
489+
shutdown_and_exit(cl, -1);
490+
}
491+
hdfsCloseFile(fs, readFile);
492+
493+
builder = hdfsOpenFileBuilderAlloc(fs, readPath);
494+
hdfsOpenFileBuilderOpt(builder, "hello", "world");
495+
496+
future = hdfsOpenFileBuilderBuild(builder);
497+
498+
readFile = hdfsOpenFileFutureGetWithTimeout(future, 1, jDays);
499+
if (!hdfsOpenFileFutureCancel(future, 0)) {
500+
fprintf(stderr, "Cancel on a completed Future should return "
501+
"false");
502+
shutdown_and_exit(cl, -1);
503+
}
504+
hdfsOpenFileFutureFree(future);
505+
506+
memset(buffer, 0, sizeof(buffer));
507+
num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
508+
sizeof(buffer));
509+
if (strncmp(fileContents, buffer, strlen(fileContents)) != 0) {
510+
fprintf(stderr, "Failed to read. Expected %s but got "
511+
"%s (%d bytes)\n", fileContents, buffer,
512+
num_read_bytes);
513+
shutdown_and_exit(cl, -1);
514+
}
515+
memset(buffer, 0, strlen(fileContents + 1));
516+
hdfsCloseFile(fs, readFile);
517+
}
518+
457519
totalResult = 0;
458520
result = 0;
459521
{

0 commit comments

Comments
 (0)