Skip to content

Commit 9ed566b

Browse files
committed
re-add the mutex on sessions.LifeTime
1 parent 5d7198c commit 9ed566b

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

cache/browser_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
)
1414

1515
func TestNoCache(t *testing.T) {
16-
t.Parallel()
1716
app := iris.New()
1817
app.Get("/", cache.NoCache, func(ctx iris.Context) {
1918
ctx.WriteString("no_cache")
@@ -30,7 +29,6 @@ func TestNoCache(t *testing.T) {
3029
}
3130

3231
func TestStaticCache(t *testing.T) {
33-
t.Parallel()
3432
// test change the time format, which is not recommended but can be done.
3533
app := iris.New().Configure(iris.WithTimeFormat("02 Jan 2006 15:04:05 GMT"))
3634

@@ -52,7 +50,7 @@ func TestStaticCache(t *testing.T) {
5250
}
5351

5452
func TestCache304(t *testing.T) {
55-
t.Parallel()
53+
// t.Parallel()
5654
app := iris.New()
5755

5856
expiresEvery := 4 * time.Second
@@ -78,7 +76,7 @@ func TestCache304(t *testing.T) {
7876
}
7977

8078
func TestETag(t *testing.T) {
81-
t.Parallel()
79+
// t.Parallel()
8280

8381
app := iris.New()
8482
n := "_"

cache/cache_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ func TestCache(t *testing.T) {
145145
}
146146
}
147147

148-
func TestCacheHandlerParallel(t *testing.T) {
149-
t.Parallel()
150-
TestCache(t)
151-
}
148+
// This works but we have issue on golog.SetLevel and get golog.Level on httptest.New
149+
// when tests are running in parallel and the loggers are used.
150+
// // TODO: Fix it on golog repository or here, we'll see.
151+
// func TestCacheHandlerParallel(t *testing.T) {
152+
// t.Parallel()
153+
// TestCache(t)
154+
// }
152155

153156
func TestCacheValidator(t *testing.T) {
154157
app := iris.New()

core/host/supervisor_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"net/http"
1212
"strings"
13+
"sync"
1314
"testing"
1415

1516
"github.com/iris-contrib/httpexpect/v2"
@@ -51,6 +52,7 @@ func newTester(t *testing.T, baseURL string, handler http.Handler) *httpexpect.E
5152
func testSupervisor(t *testing.T, creator func(*http.Server, []func(TaskHost)) *Supervisor) {
5253
loggerOutput := &bytes.Buffer{}
5354
logger := log.New(loggerOutput, "", 0)
55+
mu := new(sync.RWMutex)
5456
const (
5557
expectedHelloMessage = "Hello\n"
5658
)
@@ -78,7 +80,9 @@ func testSupervisor(t *testing.T, creator func(*http.Server, []func(TaskHost)) *
7880
}
7981

8082
helloMe := func(_ TaskHost) {
83+
mu.Lock()
8184
logger.Print(expectedHelloMessage)
85+
mu.Unlock()
8286
}
8387

8488
host := creator(srv, []func(TaskHost){helloMe})
@@ -95,7 +99,10 @@ func testSupervisor(t *testing.T, creator func(*http.Server, []func(TaskHost)) *
9599
// but it's "safe" here.
96100

97101
// testing Task (recorded) message:
98-
if got := loggerOutput.String(); expectedHelloMessage != got {
102+
mu.RLock()
103+
got := loggerOutput.String()
104+
mu.RUnlock()
105+
if expectedHelloMessage != got {
99106
t.Fatalf("expected hello Task's message to be '%s' but got '%s'", expectedHelloMessage, got)
100107
}
101108
}

sessions/lifetime.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sessions
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/kataras/iris/v12/context"
@@ -15,6 +16,8 @@ type LifeTime struct {
1516
// (this should be a bug(go1.9-rc1) or not. We don't care atm)
1617
time.Time
1718
timer *time.Timer
19+
20+
mu sync.RWMutex
1821
}
1922

2023
// Begin will begin the life based on the time.Now().Add(d).
@@ -24,8 +27,10 @@ func (lt *LifeTime) Begin(d time.Duration, onExpire func()) {
2427
return
2528
}
2629

30+
lt.mu.Lock()
2731
lt.Time = time.Now().Add(d)
2832
lt.timer = time.AfterFunc(d, onExpire)
33+
lt.mu.Unlock()
2934
}
3035

3136
// Revive will continue the life based on the stored Time.
@@ -38,24 +43,30 @@ func (lt *LifeTime) Revive(onExpire func()) {
3843
now := time.Now()
3944
if lt.Time.After(now) {
4045
d := lt.Time.Sub(now)
46+
lt.mu.Lock()
4147
lt.timer = time.AfterFunc(d, onExpire)
48+
lt.mu.Unlock()
4249
}
4350
}
4451

4552
// Shift resets the lifetime based on "d".
4653
func (lt *LifeTime) Shift(d time.Duration) {
54+
lt.mu.Lock()
4755
if d > 0 && lt.timer != nil {
4856
lt.Time = time.Now().Add(d)
4957
lt.timer.Reset(d)
5058
}
59+
lt.mu.Unlock()
5160
}
5261

5362
// ExpireNow reduce the lifetime completely.
5463
func (lt *LifeTime) ExpireNow() {
64+
lt.mu.Lock()
5565
lt.Time = context.CookieExpireDelete
5666
if lt.timer != nil {
5767
lt.timer.Stop()
5868
}
69+
lt.mu.Unlock()
5970
}
6071

6172
// HasExpired reports whether "lt" represents is expired.

0 commit comments

Comments
 (0)