diff options
author | Sven Nierlein <Sven.Nierlein@consol.de> | 2013-09-18 09:03:00 (GMT) |
---|---|---|
committer | Sven Nierlein <sven@consol.de> | 2013-09-18 09:03:00 (GMT) |
commit | 843bbfb75adf888b1cb0ee3b0dc8f98d70ff6dda (patch) | |
tree | 2b6a67036aca651e08d72a48fc37ee66ee9ee24d /NPTest.pm | |
parent | 065905e9002e1c53f82f014dfda3e134c2f9cbf1 (diff) | |
download | monitoring-plugins-843bbfb75adf888b1cb0ee3b0dc8f98d70ff6dda.tar.gz |
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.
Diffstat (limited to 'NPTest.pm')
-rw-r--r-- | NPTest.pm | 36 |
1 files changed, 19 insertions, 17 deletions
@@ -422,6 +422,7 @@ sub LoadCache | |||
422 | { | 422 | { |
423 | return if exists( $CACHE{'_cache_loaded_'} ); | 423 | return if exists( $CACHE{'_cache_loaded_'} ); |
424 | 424 | ||
425 | my $fileContents = ""; | ||
425 | if ( -f $CACHEFILENAME ) | 426 | if ( -f $CACHEFILENAME ) |
426 | { | 427 | { |
427 | my( $fileHandle ) = new IO::File; | 428 | my( $fileHandle ) = new IO::File; |
@@ -432,44 +433,45 @@ sub LoadCache | |||
432 | return; | 433 | return; |
433 | } | 434 | } |
434 | 435 | ||
435 | my( $fileContents ) = join( "\n", <$fileHandle> ); | 436 | $fileContents = join("", <$fileHandle>); |
436 | |||
437 | $fileHandle->close(); | 437 | $fileHandle->close(); |
438 | 438 | ||
439 | chomp($fileContents); | ||
439 | my( $contentsRef ) = eval $fileContents; | 440 | my( $contentsRef ) = eval $fileContents; |
440 | %CACHE = %{$contentsRef}; | 441 | %CACHE = %{$contentsRef}; |
441 | 442 | ||
442 | } | 443 | } |
443 | 444 | ||
444 | $CACHE{'_cache_loaded_'} = 1; | 445 | $CACHE{'_cache_loaded_'} = 1; |
446 | $CACHE{'_original_cache'} = $fileContents; | ||
445 | } | 447 | } |
446 | 448 | ||
447 | 449 | ||
448 | sub SaveCache | 450 | sub SaveCache |
449 | { | 451 | { |
450 | delete $CACHE{'_cache_loaded_'}; | 452 | delete $CACHE{'_cache_loaded_'}; |
453 | my $oldFileContents = delete $CACHE{'_original_cache'}; | ||
451 | 454 | ||
452 | my( $fileHandle ) = new IO::File; | 455 | my($dataDumper) = new Data::Dumper([\%CACHE]); |
453 | |||
454 | if ( ! $fileHandle->open( "> ${CACHEFILENAME}" ) ) | ||
455 | { | ||
456 | print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n"; | ||
457 | return; | ||
458 | } | ||
459 | |||
460 | my( $dataDumper ) = new Data::Dumper( [ \%CACHE ] ); | ||
461 | |||
462 | $dataDumper->Terse(1); | 456 | $dataDumper->Terse(1); |
463 | $dataDumper->Sortkeys(1); | 457 | $dataDumper->Sortkeys(1); |
464 | |||
465 | my $data = $dataDumper->Dump(); | 458 | my $data = $dataDumper->Dump(); |
466 | $data =~ s/^\s+/ /gmx; # make sure all systems use same amount of whitespace | 459 | $data =~ s/^\s+/ /gmx; # make sure all systems use same amount of whitespace |
467 | $data =~ s/^\s+}/}/gmx; | 460 | $data =~ s/^\s+}/}/gmx; |
468 | print $fileHandle $data; | 461 | chomp($data); |
469 | 462 | ||
470 | $fileHandle->close(); | 463 | if($oldFileContents ne $data) { |
464 | my($fileHandle) = new IO::File; | ||
465 | if (!$fileHandle->open( "> ${CACHEFILENAME}")) { | ||
466 | print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n"; | ||
467 | return; | ||
468 | } | ||
469 | print $fileHandle $data; | ||
470 | $fileHandle->close(); | ||
471 | } | ||
471 | 472 | ||
472 | $CACHE{'_cache_loaded_'} = 1; | 473 | $CACHE{'_cache_loaded_'} = 1; |
474 | $CACHE{'_original_cache'} = $data; | ||
473 | } | 475 | } |
474 | 476 | ||
475 | # | 477 | # |