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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
|
package lockfile
import (
"io"
"os"
"os/exec"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/containers/storage/pkg/reexec"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Warning: this is not an exhaustive set of tests.
func TestMain(m *testing.M) {
if reexec.Init() {
return
}
os.Exit(m.Run())
}
// subTouchMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the lock, waits for stdin to get closed,
// updates the last-writer for the lockfile, and then unlocks the file.
func subTouchMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockFile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.Lock()
os.Stdout.Close()
_, err = io.Copy(io.Discard, os.Stdin)
if err != nil {
logrus.Fatalf("error reading stdin: %v", err)
}
err = tf.Touch()
if err != nil {
logrus.Fatalf("error touching lock: %v", err)
}
tf.Unlock()
}
// subTouch starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired the lock. It can then signal
// that the child should Touch() the lock by closing the WriteCloser. The
// second ReadCloser will be closed when the child has finished.
// The caller must call Wait() on the returned cmd.
func subTouch(l *namedLockFile) (*exec.Cmd, io.WriteCloser, io.ReadCloser, io.ReadCloser, error) {
cmd := reexec.Command("subTouch", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, nil, err
}
ec, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, nil, err
}
return cmd, wc, rc, ec, nil
}
// subLockMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the lock, waits for stdin to get closed, and
// then unlocks the file.
func subLockMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockFile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.Lock()
os.Stdout.Close()
_, err = io.Copy(io.Discard, os.Stdin)
if err != nil {
logrus.Fatalf("error reading stdin: %v", err)
}
tf.Unlock()
}
// subLock starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired the lock. It can then signal
// that the child should release the lock by closing the WriteCloser.
// The caller must call Wait() on the returned cmd.
func subLock(l *namedLockFile) (*exec.Cmd, io.WriteCloser, io.ReadCloser, error) {
cmd := reexec.Command("subLock", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, err
}
return cmd, wc, rc, nil
}
// subRLockMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the read lock, waits for stdin to get closed,
// and then unlocks the file.
func subRLockMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockFile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.RLock()
os.Stdout.Close()
_, err = io.Copy(io.Discard, os.Stdin)
if err != nil {
logrus.Fatalf("error reading stdin: %v", err)
}
tf.Unlock()
}
// subRLock starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired a read lock. It can then signal
// that the child should release the lock by closing the WriteCloser.
// The caller must call Wait() on the returned cmd.
func subRLock(l *namedLockFile) (*exec.Cmd, io.WriteCloser, io.ReadCloser, error) {
cmd := reexec.Command("subRLock", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, err
}
return cmd, wc, rc, nil
}
func init() {
reexec.Register("subTouch", subTouchMain)
reexec.Register("subRLock", subRLockMain)
reexec.Register("subLock", subLockMain)
}
type namedLockFile struct {
*LockFile
name string
}
func getNamedLockFile(ro bool) (*namedLockFile, error) {
var l *LockFile
tf, err := os.CreateTemp("", "lockfile")
if err != nil {
return nil, err
}
name := tf.Name()
tf.Close()
if ro {
l, err = GetROLockFile(name)
} else {
l, err = GetLockFile(name)
}
if err != nil {
return nil, err
}
return &namedLockFile{LockFile: l, name: name}, nil
}
func getTempLockfile() (*namedLockFile, error) {
return getNamedLockFile(false)
}
func getTempROLockfile() (*namedLockFile, error) {
return getNamedLockFile(true)
}
func TestLockfileName(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
// l.Assert* are NOT usable for determining lock state if there are concurrent users of the lock.
// It’s just about acceptable for these smoke tests.
assert.Panics(t, l.AssertLocked)
l.RLock()
l.AssertLocked()
assert.Panics(t, l.AssertLockedForWriting)
l.Unlock()
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
l.Lock()
l.AssertLocked()
l.AssertLockedForWriting()
l.Unlock()
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
}
func TestTryWriteLockFile(t *testing.T) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
err = l.TryLock()
assert.Nil(t, err)
l.AssertLocked()
errChan := make(chan error)
go func() {
errChan <- l.TryRLock()
errChan <- l.TryLock()
}()
assert.NotNil(t, <-errChan)
assert.NotNil(t, <-errChan)
l.Unlock()
}
func TestTryReadLockFile(t *testing.T) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
err = l.TryRLock()
assert.Nil(t, err)
l.AssertLocked()
errChan := make(chan error)
go func() {
errChan <- l.TryRLock()
l.Unlock()
errChan <- l.TryLock()
}()
assert.Nil(t, <-errChan)
assert.NotNil(t, <-errChan)
l.Unlock()
go func() {
errChan <- l.TryLock()
l.Unlock()
}()
assert.Nil(t, <-errChan)
}
func TestLockfileRead(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.RLock()
l.AssertLocked()
assert.Panics(t, l.AssertLockedForWriting)
l.Unlock()
}
func TestROLockfileRead(t *testing.T) {
l, err := getTempROLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.RLock()
l.AssertLocked()
assert.Panics(t, l.AssertLockedForWriting)
l.Unlock()
}
func TestLockfileWrite(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
l.AssertLocked()
l.AssertLockedForWriting()
l.Unlock()
}
func TestROLockfileWrite(t *testing.T) {
l, err := getTempROLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
defer func() {
assert.NotNil(t, recover(), "Should have panicked trying to take a write lock using a read lock")
}()
l.Lock()
l.AssertLocked()
assert.Panics(t, l.AssertLockedForWriting)
l.Unlock()
}
func TestLockfileTouch(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
m, err := l.Modified()
require.Nil(t, err, "got an error from Modified()")
assert.True(t, m, "new lock file does not appear to have changed")
now := time.Now()
assert.False(t, l.TouchedSince(now), "file timestamp was updated for no reason")
time.Sleep(2 * time.Second)
err = l.Touch()
require.Nil(t, err, "got an error from Touch()")
assert.True(t, l.TouchedSince(now), "file timestamp was not updated by Touch()")
m, err = l.Modified()
require.Nil(t, err, "got an error from Modified()")
assert.False(t, m, "lock file mistakenly indicated that someone else has modified it")
cmd, stdin, stdout, stderr, err := subTouch(l)
require.Nil(t, err, "got an error starting a subprocess to touch the lockfile")
l.Unlock()
_, err = io.Copy(io.Discard, stdout)
require.NoError(t, err)
stdin.Close()
_, err = io.Copy(io.Discard, stderr)
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
l.Lock()
m, err = l.Modified()
l.Unlock()
require.Nil(t, err, "got an error from Modified()")
assert.True(t, m, "lock file failed to notice that someone else modified it")
}
func TestLockfileRecordWrite(t *testing.T) {
l, err := getTempLockfile()
require.NoError(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
state, err := l.GetLastWrite()
require.NoError(t, err)
state1, m, err := l.ModifiedSince(state)
require.NoError(t, err)
assert.False(t, m)
now := time.Now()
assert.False(t, l.TouchedSince(now), "file timestamp was updated for no reason")
// state1 = before write
time.Sleep(2 * time.Second)
state2, err := l.RecordWrite()
require.NoError(t, err)
assert.True(t, l.TouchedSince(now))
// state2 = outcome of the write
// It is possible, and valid, to retain earlier state values and compare them with the current state:
state3, m, err := l.ModifiedSince(state1)
require.NoError(t, err)
assert.True(t, m)
state4, m, err := l.ModifiedSince(state2)
require.NoError(t, err)
assert.False(t, m)
// Undocumented: the internals of LastWrite can be compared
assert.Equal(t, state4, state3)
cmd, stdin, stdout, stderr, err := subTouch(l)
require.Nil(t, err, "got an error starting a subprocess to touch the lockfile")
l.Unlock()
_, err = io.Copy(io.Discard, stdout)
require.NoError(t, err)
stdin.Close()
_, err = io.Copy(io.Discard, stderr)
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
l.Lock()
_, m, err = l.ModifiedSince(state4)
l.Unlock()
require.NoError(t, err)
assert.True(t, m, "lock file failed to notice that someone else modified it")
}
func TestLockfileWriteConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var highestMutex sync.Mutex
var counter, highest int64
for range 8000 {
wg.Add(1)
go func() {
l.Lock()
workingCounter := atomic.AddInt64(&counter, 1)
assert.True(t, workingCounter >= 0, "counter should never be less than zero")
highestMutex.Lock()
if workingCounter > highest {
// multiple writers should not be able to hold
// this lock at the same time, so there should
// be no point at which two goroutines are
// between the AddInt64() above and the one
// below
highest = workingCounter
}
highestMutex.Unlock()
atomic.AddInt64(&counter, -1)
l.Unlock()
wg.Done()
}()
}
wg.Wait()
assert.True(t, highest == 1, "counter should never have gone above 1, got to %d", highest)
}
func TestLockfileReadConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
// the test below is inspired by the stdlib's rwmutex tests
numReaders := 1000
locked := make(chan bool)
unlocked := make(chan bool)
done := make(chan bool)
for range numReaders {
go func() {
l.RLock()
locked <- true
<-unlocked
l.Unlock()
done <- true
}()
}
// Wait for all parallel locks to succeed
for range numReaders {
<-locked
}
// Instruct all parallel locks to unlock
for range numReaders {
unlocked <- true
}
// Wait for all parallel locks to be unlocked
for range numReaders {
<-done
}
}
func TestLockfileMixedConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
counter := int32(0)
diff := int32(10000)
numIterations := 10
numReaders := 100
numWriters := 50
done := make(chan bool)
// A writer always adds `diff` to the counter. Hence, `diff` is the
// only valid value in the critical section.
writer := func(c *int32) {
for range numIterations {
l.Lock()
tmp := atomic.AddInt32(c, diff)
assert.True(t, tmp == diff, "counter should be %d but instead is %d", diff, tmp)
time.Sleep(100 * time.Millisecond)
atomic.AddInt32(c, diff*(-1))
l.Unlock()
}
done <- true
}
// A reader always adds `1` to the counter. Hence,
// [1,`numReaders*numIterations`] are valid values.
reader := func(c *int32) {
for range numIterations {
l.RLock()
tmp := atomic.AddInt32(c, 1)
assert.True(t, tmp >= 1 && tmp < diff)
time.Sleep(100 * time.Millisecond)
atomic.AddInt32(c, -1)
l.Unlock()
}
done <- true
}
for i := range numReaders {
go reader(&counter)
// schedule a writer every 2nd iteration
if i%2 == 1 {
go writer(&counter)
}
}
for range numReaders + numWriters {
<-done
}
}
func TestLockfileMultiprocessRead(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var rcounter, rhighest int64
var highestMutex sync.Mutex
subs := make([]struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
}, 100)
for i := range subs {
cmd, stdin, stdout, err := subRLock(l)
require.Nil(t, err, "error starting subprocess %d to take a read lock", i+1)
subs[i].cmd = cmd
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
_, err := io.Copy(io.Discard, subs[i].stdout)
require.NoError(t, err)
if testing.Verbose() {
t.Logf("\tchild %4d acquired the read lock\n", i+1)
}
workingRcounter := atomic.AddInt64(&rcounter, 1)
highestMutex.Lock()
if workingRcounter > rhighest {
rhighest = workingRcounter
}
highestMutex.Unlock()
time.Sleep(1 * time.Second)
atomic.AddInt64(&rcounter, -1)
if testing.Verbose() {
t.Logf("\ttelling child %4d to release the read lock\n", i+1)
}
subs[i].stdin.Close()
err = subs[i].cmd.Wait()
require.NoError(t, err)
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, rhighest > 1, "expected to have multiple reader locks at least once, only had %d", rhighest)
}
func TestLockfileMultiprocessWrite(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var wcounter, whighest int64
var highestMutex sync.Mutex
subs := make([]struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
}, 10)
for i := range subs {
cmd, stdin, stdout, err := subLock(l)
require.Nil(t, err, "error starting subprocess %d to take a write lock", i+1)
subs[i].cmd = cmd
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
_, err := io.Copy(io.Discard, subs[i].stdout)
require.NoError(t, err)
if testing.Verbose() {
t.Logf("\tchild %4d acquired the write lock\n", i+1)
}
workingWcounter := atomic.AddInt64(&wcounter, 1)
highestMutex.Lock()
if workingWcounter > whighest {
whighest = workingWcounter
}
highestMutex.Unlock()
time.Sleep(1 * time.Second)
atomic.AddInt64(&wcounter, -1)
if testing.Verbose() {
t.Logf("\ttelling child %4d to release the write lock\n", i+1)
}
subs[i].stdin.Close()
err = subs[i].cmd.Wait()
require.NoError(t, err)
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, whighest == 1, "expected to have no more than one writer lock active at a time, had %d", whighest)
}
func TestLockfileMultiprocessMixed(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var rcounter, wcounter, rhighest, whighest int64
var rhighestMutex, whighestMutex sync.Mutex
const (
biasP = 1
biasQ = 10
groups = 15
)
writer := func(i int) bool { return (i % biasQ) < biasP }
subs := make([]struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
}, biasQ*groups)
for i := range subs {
var cmd *exec.Cmd
var stdin io.WriteCloser
var stdout io.ReadCloser
if writer(i) {
cmd, stdin, stdout, err = subLock(l)
require.Nil(t, err, "error starting subprocess %d to take a write lock", i+1)
} else {
cmd, stdin, stdout, err = subRLock(l)
require.Nil(t, err, "error starting subprocess %d to take a read lock", i+1)
}
subs[i].cmd = cmd
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
// wait for the child to acquire whatever lock it wants
_, err := io.Copy(io.Discard, subs[i].stdout)
require.NoError(t, err)
if writer(i) {
// child acquired a write lock
if testing.Verbose() {
t.Logf("\tchild %4d acquired the write lock\n", i+1)
}
workingWcounter := atomic.AddInt64(&wcounter, 1)
whighestMutex.Lock()
if workingWcounter > whighest {
whighest = workingWcounter
}
workingRcounter := atomic.LoadInt64(&rcounter)
require.Zero(t, workingRcounter, "acquired a write lock while we appear to have read locks")
whighestMutex.Unlock()
} else {
// child acquired a read lock
if testing.Verbose() {
t.Logf("\tchild %4d acquired the read lock\n", i+1)
}
workingRcounter := atomic.AddInt64(&rcounter, 1)
rhighestMutex.Lock()
if workingRcounter > rhighest {
rhighest = workingRcounter
}
workingWcounter := atomic.LoadInt64(&wcounter)
require.Zero(t, workingWcounter, "acquired a read lock while we appear to have write locks")
rhighestMutex.Unlock()
}
time.Sleep(1 * time.Second)
if writer(i) {
atomic.AddInt64(&wcounter, -1)
if testing.Verbose() {
t.Logf("\ttelling child %4d to release the write lock\n", i+1)
}
} else {
atomic.AddInt64(&rcounter, -1)
if testing.Verbose() {
t.Logf("\ttelling child %4d to release the read lock\n", i+1)
}
}
subs[i].stdin.Close()
err = subs[i].cmd.Wait()
require.NoError(t, err)
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, rhighest > 1, "expected to have more than one reader lock active at a time at least once, only had %d", rhighest)
assert.True(t, whighest == 1, "expected to have no more than one writer lock active at a time, had %d", whighest)
}
func TestLockfileMultiprocessModified(t *testing.T) {
lock, err := getTempLockfile()
require.NoError(t, err, "creating lock")
// Lock hasn't been touched yet - initial state.
lock.Lock()
modified, err := lock.Modified()
lock.Unlock()
assert.NoError(t, err, "checking if lock was modified")
assert.True(t, modified, "expected Modified() to be true before anyone Touched it")
lock.Lock()
modified, err = lock.Modified()
lock.Unlock()
assert.NoError(t, err, "checking if lock was modified")
assert.False(t, modified, "expected Modified() to be false after we check the first time, before anyone Touched it")
// Take a read lock somewhere, then see if we incorrectly detect changes.
cmd, wc, rc1, err := subLock(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
lock.Lock()
modified, err = lock.Modified()
lock.Unlock()
assert.NoError(t, err, "checking if lock was modified")
assert.False(t, modified, "expected Modified() to be false after someone else locked but did not Touch it")
// Take a write lock somewhere, then see if we correctly detect changes.
cmd, wc, rc1, rc2, err := subTouch(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
rc2.Close()
lock.Lock()
modified, err = lock.Modified()
lock.Unlock()
assert.NoError(t, err, "checking if lock was modified")
assert.True(t, modified, "expected Modified() to be true after someone else Touched it")
// Take a read lock somewhere, then see if we incorrectly detect changes.
cmd, wc, rc1, err = subLock(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
lock.Lock()
modified, err = lock.Modified()
lock.Unlock()
assert.NoError(t, err, "checking if lock was modified")
assert.False(t, modified, "expected Modified() to be false after someone else locked but did not Touch it")
}
func TestLockfileMultiprocessModifiedSince(t *testing.T) {
lock, err := getTempLockfile()
require.NoError(t, err, "creating lock")
// Lock hasn't been touched yet - initial state.
lock.Lock()
state, err := lock.GetLastWrite()
require.NoError(t, err)
state, modified, err := lock.ModifiedSince(state)
lock.Unlock()
require.NoError(t, err)
assert.False(t, modified)
lock.Lock()
state, modified, err = lock.ModifiedSince(state)
lock.Unlock()
require.NoError(t, err)
assert.False(t, modified)
// Take a read lock somewhere, then see if we incorrectly detect changes.
cmd, wc, rc1, err := subLock(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
lock.Lock()
state, modified, err = lock.ModifiedSince(state)
lock.Unlock()
require.NoError(t, err)
assert.False(t, modified)
// Take a write lock somewhere, then see if we correctly detect changes.
cmd, wc, rc1, rc2, err := subTouch(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
rc2.Close()
lock.Lock()
state, modified, err = lock.ModifiedSince(state)
lock.Unlock()
require.NoError(t, err)
assert.True(t, modified)
// Take a read lock somewhere, then see if we incorrectly detect changes.
cmd, wc, rc1, err = subLock(lock)
require.NoError(t, err)
wc.Close()
err = cmd.Wait()
require.NoError(t, err)
rc1.Close()
lock.Lock()
_, modified, err = lock.ModifiedSince(state)
lock.Unlock()
require.NoError(t, err)
assert.False(t, modified)
}
|