Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Mar 9, 2021
2 parents 16aca55 + aa5480a commit 9e17f42
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.12, 1.13, 1.14, ^1.14]
go-version: [1.12, 1.13, 1.14, 1.15, ^1.16]
steps:

- name: Set up Go 1.x
Expand All @@ -29,7 +29,7 @@ jobs:
go get -v -t -d ./...
- name: Run Tests
run: go test -v -cover ./...
run: go test -v -timeout 300s -cover ./...

- name: Run Benchmarks
run: go test -bench=. -benchmem
run: go test -timeout 300s -bench=. -benchmem
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v1.3.1

- Fix `concurrent map read and map write` panic when accessing the map concurrently while getting an existing key, which is expired at the time. [#4]
2 changes: 2 additions & 0 deletions timedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ func (tm *TimedMap) get(key interface{}, sec int) *element {
}

if time.Now().After(v.expires) {
tm.mtx.Lock()
defer tm.mtx.Unlock()
tm.expireElement(key, sec, v)
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions timedmap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package timedmap

import (
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -275,6 +276,27 @@ func TestConcurrentReadWrite(t *testing.T) {
time.Sleep(1 * time.Second)
}

func TestGetExpiredConcurrent(t *testing.T) {
tm := New(dCleanupTick)

wg := sync.WaitGroup{}
for i := 0; i < 50000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tm.Set(1, 1, 0)
}()

wg.Add(1)
go func() {
defer wg.Done()
tm.GetValue(1)
}()
}

wg.Wait()
}

func TestExternalTicker(t *testing.T) {
const key = "tKeySet"
const val = "tValSet"
Expand Down

0 comments on commit 9e17f42

Please sign in to comment.