github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/helpers/lock/processManager_test.go (about)

     1  package lock
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	. "gopkg.in/check.v1"
     8  )
     9  
    10  type ProcessManagerTests struct {
    11  	manager        *ProcessManager
    12  	repositoryPath string
    13  }
    14  
    15  var _ = Suite(&ProcessManagerTests{})
    16  
    17  func (t *ProcessManagerTests) SetUpTest(c *C) {
    18  	t.manager = newProcessManager()
    19  	t.repositoryPath = "/repository/path"
    20  }
    21  
    22  func (t *ProcessManagerTests) TestGetRole(c *C) {
    23  	locker := t.manager.Get(t.repositoryPath, "lock")
    24  	locker.Lock()
    25  	locker.Unlock()
    26  }
    27  
    28  func (t *ProcessManagerTests) TestGetSame(c *C) {
    29  	locker := t.manager.Get(t.repositoryPath, "lock")
    30  	c.Assert(locker, Equals, t.manager.Get(t.repositoryPath, "lock"))
    31  }
    32  
    33  func (t *ProcessManagerTests) TestReset(c *C) {
    34  	locker := t.manager.Get(t.repositoryPath, "lock")
    35  	t.manager.reset()
    36  	c.Assert(locker, Not(Equals), t.manager.Get(t.repositoryPath, "lock"))
    37  }
    38  
    39  func (t *ProcessManagerTests) TestDifferentRepositories(c *C) {
    40  	locker := t.manager.Get(t.repositoryPath, "lock")
    41  	locker2 := t.manager.Get("/repository/otherpath", "lock")
    42  	c.Assert(locker, Not(Equals), locker2)
    43  }
    44  
    45  func (t *ProcessManagerTests) TestDifferentRoles(c *C) {
    46  	locker := t.manager.Get(t.repositoryPath, "lock")
    47  	locker2 := t.manager.Get(t.repositoryPath, "other_role")
    48  	c.Assert(locker, Not(Equals), locker2)
    49  }
    50  
    51  func (t *ProcessManagerTests) TestLockInteraction(c *C) {
    52  	locker := t.manager.Get(t.repositoryPath, "lock")
    53  	locker.Lock()
    54  	sleepTime := 10 * time.Millisecond
    55  
    56  	var (
    57  		unlockTime time.Time
    58  		relockTime time.Time
    59  	)
    60  	go func() {
    61  		sameLocker := t.manager.Get(t.repositoryPath, "lock")
    62  		sameLocker.Lock()
    63  		// On windows the internal Clock has some issues with very
    64  		// short time spans. That is why we are waiting here another
    65  		// amount of time before writing down the lock time.
    66  		if runtime.GOOS == "windows" {
    67  			time.Sleep(200 * time.Millisecond)
    68  		}
    69  		relockTime = time.Now()
    70  		sameLocker.Unlock()
    71  	}()
    72  
    73  	time.Sleep(sleepTime)
    74  	unlockTime = time.Now()
    75  	locker.Unlock()
    76  	time.Sleep(sleepTime)
    77  	locker.Lock()
    78  	c.Assert(relockTime, NotNil)
    79  	delta := relockTime.Sub(unlockTime)
    80  	c.Assert(
    81  		delta.Nanoseconds() > 0, Equals, true,
    82  		Commentf("Seconds passed %.2f must be positive", delta.Seconds()))
    83  	locker.Unlock()
    84  }