Skip to content

Commit

Permalink
merge origin/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Nov 19, 2020
2 parents 22b36f2 + dc29725 commit c13046c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package timedmap

import "errors"

var (
ErrKeyNotFound = errors.New("key not found")
)
3 changes: 1 addition & 2 deletions section.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package timedmap

import (
"errors"
"time"
)

Expand Down Expand Up @@ -90,7 +89,7 @@ func (s *section) GetValue(key interface{}) interface{} {
func (s *section) GetExpires(key interface{}) (time.Time, error) {
v := s.tm.get(key, s.sec)
if v == nil {
return time.Time{}, errors.New("key not found")
return time.Time{}, ErrKeyNotFound
}
return v.expires, nil
}
Expand Down
9 changes: 5 additions & 4 deletions section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestSectionGetExpire(t *testing.T) {
s.Set(key, val, 50*time.Millisecond)
ct := time.Now().Add(50 * time.Millisecond)

if _, err := s.GetExpires("keyNotExists"); err.Error() != "key not found" {
if _, err := s.GetExpires("keyNotExists"); err != ErrKeyNotFound {
t.Fatal("err was not 'key not found': ", err)
}

Expand All @@ -120,8 +120,9 @@ func TestSectionSetExpire(t *testing.T) {

s := tm.Section(sec)

if err := s.Refresh("keyNotExists", time.Hour); err == nil || err.Error() != "key not found" {
t.Fatalf("error on non existing key was %v != 'key not found'", err)
if err := tm.SetExpire("notExistentKey", 1*time.Second); err != ErrKeyNotFound {
t.Errorf("returned error should have been '%s', but was '%s'",
ErrKeyNotFound.Error(), err.Error())
}

s.Set(key, 1, 12*time.Millisecond)
Expand Down Expand Up @@ -194,7 +195,7 @@ func TestSectionRefresh(t *testing.T) {

s := tm.Section(sec)

if err := s.Refresh("keyNotExists", time.Hour); err == nil || err.Error() != "key not found" {
if err := s.Refresh("keyNotExists", time.Hour); err == nil || err != ErrKeyNotFound {
t.Fatalf("error on non existing key was %v != 'key not found'", err)
}

Expand Down
7 changes: 3 additions & 4 deletions timedmap.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package timedmap

import (
"errors"
"sync"
"time"
)
Expand Down Expand Up @@ -108,7 +107,7 @@ func (tm *TimedMap) GetValue(key interface{}) interface{} {
func (tm *TimedMap) GetExpires(key interface{}) (time.Time, error) {
v := tm.get(key, 0)
if v == nil {
return time.Time{}, errors.New("key not found")
return time.Time{}, ErrKeyNotFound
}
return v.expires, nil
}
Expand Down Expand Up @@ -274,7 +273,7 @@ func (tm *TimedMap) remove(key interface{}, sec int) {
func (tm *TimedMap) refresh(key interface{}, sec int, d time.Duration) error {
v := tm.get(key, sec)
if v == nil {
return errors.New("key not found")
return ErrKeyNotFound
}
v.expires = v.expires.Add(d)
return nil
Expand All @@ -285,7 +284,7 @@ func (tm *TimedMap) refresh(key interface{}, sec int, d time.Duration) error {
func (tm *TimedMap) setExpire(key interface{}, sec int, d time.Duration) error {
v := tm.get(key, sec)
if v == nil {
return errors.New("key not found")
return ErrKeyNotFound
}
v.expires = time.Now().Add(d)
return nil
Expand Down
26 changes: 23 additions & 3 deletions timedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestGetExpire(t *testing.T) {
tm.Set(key, val, 50*time.Millisecond)
ct := time.Now().Add(50 * time.Millisecond)

if _, err := tm.GetExpires("keyNotExists"); err.Error() != "key not found" {
if _, err := tm.GetExpires("keyNotExists"); err != ErrKeyNotFound {
t.Fatal("err was not 'key not found': ", err)
}

Expand All @@ -117,10 +117,15 @@ func TestSetExpire(t *testing.T) {

tm := New(dCleanupTick)

if err := tm.Refresh("keyNotExists", time.Hour); err == nil || err.Error() != "key not found" {
if err := tm.Refresh("keyNotExists", time.Hour); err == nil || err != ErrKeyNotFound {
t.Fatalf("error on non existing key was %v != 'key not found'", err)
}

if err := tm.SetExpire("notExistentKey", 1*time.Second); err != ErrKeyNotFound {
t.Errorf("returned error should have been '%s', but was '%s'",
ErrKeyNotFound.Error(), err.Error())
}

tm.Set(key, 1, 12*time.Millisecond)
if err := tm.SetExpire(key, 50*time.Millisecond); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -182,7 +187,7 @@ func TestRefresh(t *testing.T) {

tm := New(dCleanupTick)

if err := tm.Refresh("keyNotExists", time.Hour); err == nil || err.Error() != "key not found" {
if err := tm.Refresh("keyNotExists", time.Hour); err == nil || err != ErrKeyNotFound {
t.Fatalf("error on non existing key was %v != 'key not found'", err)
}

Expand Down Expand Up @@ -289,6 +294,21 @@ func TestExternalTicker(t *testing.T) {
}
}

func TestBeforeCleanup(t *testing.T) {
const key, value = 1, 2

tm := New(1 * time.Hour)

tm.Set(key, value, 5*time.Millisecond)

time.Sleep(10 * time.Millisecond)

_, ok := tm.GetValue(key).(int)
if ok {
t.Fatal("value got recovered but should have been expired")
}
}

// ----------------------------------------------------------
// --- BENCHMARKS ---

Expand Down

0 comments on commit c13046c

Please sign in to comment.