[nagiosplug] tests: only write cache file if it changed
"Nagios Plugin Development" <[email protected]> Wed, 18 Sep 2013 09:30:25 +0000
| Newsgroups | gmane.network.nagios.plugins.cvs |
|---|---|
| Message-ID | <[email protected]> |
Module: nagiosplug
Branch: master
Commit: 843bbfb75adf888b1cb0ee3b0dc8f98d70ff6dda
Author: Sven Nierlein <[email protected]>
Committer: Sven Nierlein <[email protected]>
Date: Wed Sep 18 11:03:00 2013 +0200
URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=843bbfb
tests: only write cache file if it changed
Tests sometimes fila when running multiple parallel tests using the
same cache file because it is written everytime a test parameter
is read. Since there is no locking, this might fail from time to time.
---
NPTest.pm | 36 +++++++++++++++++++-----------------
1 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/NPTest.pm b/NPTest.pm
index 0713b5e..9b85617 100644
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -422,6 +422,7 @@ sub LoadCache
{
return if exists( $CACHE{'_cache_loaded_'} );
+ my $fileContents = "";
if ( -f $CACHEFILENAME )
{
my( $fileHandle ) = new IO::File;
@@ -432,44 +433,45 @@ sub LoadCache
return;
}
- my( $fileContents ) = join( "\n", <$fileHandle> );
-
+ $fileContents = join("", <$fileHandle>);
$fileHandle->close();
+ chomp($fileContents);
my( $contentsRef ) = eval $fileContents;
%CACHE = %{$contentsRef};
}
- $CACHE{'_cache_loaded_'} = 1;
+ $CACHE{'_cache_loaded_'} = 1;
+ $CACHE{'_original_cache'} = $fileContents;
}
sub SaveCache
{
delete $CACHE{'_cache_loaded_'};
+ my $oldFileContents = delete $CACHE{'_original_cache'};
- my( $fileHandle ) = new IO::File;
-
- if ( ! $fileHandle->open( "> ${CACHEFILENAME}" ) )
- {
- print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n";
- return;
- }
-
- my( $dataDumper ) = new Data::Dumper( [ \%CACHE ] );
-
+ my($dataDumper) = new Data::Dumper([\%CACHE]);
$dataDumper->Terse(1);
$dataDumper->Sortkeys(1);
-
my $data = $dataDumper->Dump();
$data =~ s/^\s+/ /gmx; # make sure all systems use same amount of whitespace
$data =~ s/^\s+}/}/gmx;
- print $fileHandle $data;
+ chomp($data);
- $fileHandle->close();
+ if($oldFileContents ne $data) {
+ my($fileHandle) = new IO::File;
+ if (!$fileHandle->open( "> ${CACHEFILENAME}")) {
+ print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n";
+ return;
+ }
+ print $fileHandle $data;
+ $fileHandle->close();
+ }
- $CACHE{'_cache_loaded_'} = 1;
+ $CACHE{'_cache_loaded_'} = 1;
+ $CACHE{'_original_cache'} = $data;
}
#
------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13.
http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk