mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 11:48:43 -07:00
Fix dirstore code and speed up some tests
When using Unix() time, since it is number of seconds, it is better to round up the time before adding a ttl. Trying to shorten some of the tests showed that in some cases a file was removed too early. This was because the computed expiration with ttl fell in the same second, so the file was removed prematurely. So anywhere where we used to do: time.Now().Addd(ttl).Unix(), I changed to time.Now().Round(time.Second).Add(ttl).Unix(). I was able to reduce the time of TestTTL from 21 seconds down to less than 5. TestExpiration was also shorten. Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
@@ -566,7 +566,7 @@ func (pq *expirationTracker) updateTrack(publicKey string) {
|
||||
i := e.Value.(*jwtItem)
|
||||
if pq.ttl != 0 {
|
||||
// only update expiration when set
|
||||
i.expiration = time.Now().Add(pq.ttl).Unix()
|
||||
i.expiration = time.Now().Round(time.Second).Add(pq.ttl).Unix()
|
||||
heap.Fix(pq, i.index)
|
||||
}
|
||||
if pq.evictOnLimit {
|
||||
@@ -590,7 +590,7 @@ func (pq *expirationTracker) track(publicKey string, hash *[sha256.Size]byte, th
|
||||
if pq.ttl == time.Duration(math.MaxInt64) {
|
||||
exp = math.MaxInt64
|
||||
} else {
|
||||
exp = time.Now().Add(pq.ttl).Unix()
|
||||
exp = time.Now().Round(time.Second).Add(pq.ttl).Unix()
|
||||
}
|
||||
} else {
|
||||
if g, err := jwt.DecodeGeneric(theJWT); err == nil {
|
||||
|
||||
@@ -401,7 +401,7 @@ func createTestAccount(t *testing.T, dirStore *DirJWTStore, expSec int, accKey n
|
||||
require_NoError(t, err)
|
||||
account := jwt.NewAccountClaims(pubKey)
|
||||
if expSec > 0 {
|
||||
account.Expires = time.Now().Add(time.Second * time.Duration(expSec)).Unix()
|
||||
account.Expires = time.Now().Round(time.Second).Add(time.Second * time.Duration(expSec)).Unix()
|
||||
}
|
||||
jwt, err := account.Encode(accKey)
|
||||
require_NoError(t, err)
|
||||
@@ -438,20 +438,20 @@ func TestExpiration(t *testing.T) {
|
||||
|
||||
h := dirStore.Hash()
|
||||
|
||||
for i := 1; i <= 5; i++ {
|
||||
account(i * 2)
|
||||
for i := 1; i <= 3; i++ {
|
||||
account(i)
|
||||
nh := dirStore.Hash()
|
||||
require_NotEqual(t, h, nh)
|
||||
h = nh
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
for i := 5; i > 0; i-- {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
for i := 3; i > 0; i-- {
|
||||
f, err := ioutil.ReadDir(dir)
|
||||
require_NoError(t, err)
|
||||
require_Len(t, len(f), i)
|
||||
assertStoreSize(t, dirStore, i)
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
time.Sleep(time.Second)
|
||||
|
||||
nh := dirStore.Hash()
|
||||
require_NotEqual(t, h, nh)
|
||||
@@ -625,7 +625,7 @@ func TestLru(t *testing.T) {
|
||||
_, err = os.Stat(fmt.Sprintf("%s/%s.jwt", dir, pKey3))
|
||||
require_True(t, os.IsNotExist(err))
|
||||
// let key1 expire
|
||||
time.Sleep(2 * time.Second)
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
assertStoreSize(t, dirStore, 1)
|
||||
_, err = os.Stat(fmt.Sprintf("%s/%s.jwt", dir, pKey1))
|
||||
require_True(t, os.IsNotExist(err))
|
||||
@@ -762,7 +762,7 @@ func TestTTL(t *testing.T) {
|
||||
require_NoError(t, err)
|
||||
require_Len(t, len(f), 1)
|
||||
}
|
||||
dirStore, err := NewExpiringDirJWTStore(dir, false, false, time.Millisecond*100, 10, true, 2*time.Second, nil)
|
||||
dirStore, err := NewExpiringDirJWTStore(dir, false, false, 100*time.Millisecond, 10, true, time.Second, nil)
|
||||
require_NoError(t, err)
|
||||
defer dirStore.Close()
|
||||
|
||||
@@ -772,22 +772,22 @@ func TestTTL(t *testing.T) {
|
||||
require_NoError(t, err)
|
||||
jwt := createTestAccount(t, dirStore, 0, accountKey)
|
||||
require_OneJWT()
|
||||
for i := 0; i < 6; i++ {
|
||||
time.Sleep(time.Second)
|
||||
for i := 0; i < 3; i++ {
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
dirStore.LoadAcc(pubKey)
|
||||
require_OneJWT()
|
||||
}
|
||||
for i := 0; i < 6; i++ {
|
||||
time.Sleep(time.Second)
|
||||
for i := 0; i < 3; i++ {
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
dirStore.SaveAcc(pubKey, jwt)
|
||||
require_OneJWT()
|
||||
}
|
||||
for i := 0; i < 6; i++ {
|
||||
time.Sleep(time.Second)
|
||||
for i := 0; i < 3; i++ {
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
createTestAccount(t, dirStore, 0, accountKey)
|
||||
require_OneJWT()
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
time.Sleep(2 * time.Second)
|
||||
f, err := ioutil.ReadDir(dir)
|
||||
require_NoError(t, err)
|
||||
require_Len(t, len(f), 0)
|
||||
|
||||
Reference in New Issue
Block a user