Skip to content

Commit

Permalink
Tie::File: use File::Temp in two test scripts
Browse files Browse the repository at this point in the history
t/29_downcopy.t and t/29a_upcopy.t intermittently timeout on some
smokers. This is speculated to be due to the current directory being on
a very slow USB drive, when lots of small test files are created,
modified and deleted by these tests. So making these tests I/O bound
rather than CPU bound.

Update these two test scripts to use File::Temp to create a temp subdir in
the OS's normal temp directory as a place for the temp files, rather
than just the current directory.

This has two potential advantages. First the OS's normal tempdir (e.g.
/tmp or wherever $TMPDIR points to) may be mounted as tmpfs or
similar, and thus won't actually write to a slow USB or network drive.
Second, each test file will be created in directory that's normally
empty, so there's potentially less manipulating of the directory
information on disk.

In this thread,

    http://nntp.perl.org/group/perl.perl5.porters/267991

the wallclock time taken for 29_downcopy.t on a raspberry pi reduced
from 55.29s to 0.91s with the application of this patch.
  • Loading branch information
iabyn committed Feb 27, 2024
1 parent 8f77082 commit 0dcb31c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
27 changes: 16 additions & 11 deletions dist/Tie-File/t/29_downcopy.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use strict;
use warnings;

use File::Temp ();

#
# Unit tests of _downcopy function
#
Expand All @@ -11,10 +13,16 @@ use warnings;
# moving everything in the block forwards to make room.
# Instead of writing the last length($data) bytes from the block
# (because there isn't room for them any longer) return them.
#
#

my $file = "tf29-$$.txt";
# Make a temp dir under the OS's normal temp directory for creating
# test files in. By using the OS's temp dir rather than the current
# directory, we increase the chances that the tests are run on a tmpfs
# file system or similar. This becomes important when the current
# directory is on a very slow USB drive for example, as this test file
# does lots of file creating, modifying and deleting.

my $tempdir = File::Temp::tempdir("Tie-File-XXXXXX",
TMPDIR => 1, CLEANUP => 1);

print "1..718\n";

Expand Down Expand Up @@ -256,8 +264,9 @@ sub try0 {
map { defined $_ ? $_ : 'undef' }
$pos, $len, $newlen, $FLEN, $line;

open F, '>', $file or die "Couldn't open file $file: $!";
binmode F;
my ($fh, $file) = File::Temp::tempfile("29-XXXXX", DIR => $tempdir);

binmode $fh;

# The record has exactly 17 characters. This will help ensure that
# even if _downcopy screws up, the data doesn't coincidentally
Expand All @@ -269,8 +278,8 @@ sub try0 {
my $oldfile = $d x $recs;
my $flen = defined($FLEN) ? $FLEN : $recs * 17;
substr($oldfile, $FLEN) = "" if defined $FLEN; # truncate
print F $oldfile;
close F;
print $fh $oldfile;
close $fh;

die "wrong length!" unless -s $file == $flen;

Expand Down Expand Up @@ -326,7 +335,3 @@ sub try0 {
print $a_retval eq $x_retval ? "ok $N - ret $desc\n" : "not ok $N - ret $desc\n";
$N++;
}

END {
1 while unlink $file;
}
25 changes: 16 additions & 9 deletions dist/Tie-File/t/29a_upcopy.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use strict;
use warnings;

use File::Temp ();

#
# Unit tests of _upcopy function
#
Expand All @@ -14,7 +16,15 @@ use warnings;
# but the source and destination regions may overlap.)


my $file = "tf29a-$$.txt";
# Make a temp dir under the OS's normal temp directory for creating
# test files in. By using the OS's temp dir rather than the current
# directory, we increase the chances that the tests are run on a tmpfs
# file system or similar. This becomes important when the current
# directory is on a very slow USB drive for example, as this test file
# does lots of file creating, modifying and deleting.

my $tempdir = File::Temp::tempdir("Tie-File-XXXXXX",
TMPDIR => 1, CLEANUP => 1);

print "1..55\n";

Expand Down Expand Up @@ -108,8 +118,9 @@ sub try {
map { defined $_ ? $_ : 'undef' }
$src, $dst, $len, $FLEN, $line;

open F, '>', $file or die "Couldn't open file $file: $!";
binmode F;
my ($fh, $file) = File::Temp::tempfile("29A-XXXXX", DIR => $tempdir);

binmode $fh;

# The record has exactly 17 characters. This will help ensure that
# even if _upcopy screws up, the data doesn't coincidentally
Expand All @@ -121,8 +132,8 @@ sub try {
my $oldfile = $d x $recs;
my $flen = defined($FLEN) ? $FLEN : $recs * 17;
substr($oldfile, $FLEN) = "" if defined $FLEN; # truncate
print F $oldfile;
close F;
print $fh $oldfile;
close $fh;

die "wrong length!" unless -s $file == $flen;

Expand Down Expand Up @@ -175,7 +186,3 @@ sub ctrlfix {
s/\r/\\r/g;
}
}

END {
1 while unlink $file;
}

0 comments on commit 0dcb31c

Please sign in to comment.