-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add clock Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Add clock tests Signed-off-by: Vladimir Popov <vladimir.popov@xored.com> * Extract clockmock from clock Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
- Loading branch information
Vladimir Popov
authored
Mar 26, 2021
1 parent
8fe930c
commit 18c7663
Showing
11 changed files
with
783 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package clock provides tools for accessing time functions | ||
package clock | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// Clock is an interface for accessing time functions | ||
type Clock interface { | ||
Now() time.Time | ||
Since(t time.Time) time.Duration | ||
Until(t time.Time) time.Duration | ||
|
||
Sleep(d time.Duration) | ||
|
||
Timer(d time.Duration) Timer | ||
After(d time.Duration) <-chan time.Time | ||
AfterFunc(d time.Duration, f func()) Timer | ||
|
||
Ticker(d time.Duration) Ticker | ||
|
||
WithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc) | ||
WithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) | ||
} | ||
|
||
type clockImpl struct{} | ||
|
||
func (c *clockImpl) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (c *clockImpl) Since(t time.Time) time.Duration { | ||
return time.Since(t) | ||
} | ||
|
||
func (c *clockImpl) Until(t time.Time) time.Duration { | ||
return time.Until(t) | ||
} | ||
|
||
func (c *clockImpl) Sleep(d time.Duration) { | ||
time.Sleep(d) | ||
} | ||
|
||
func (c *clockImpl) Timer(d time.Duration) Timer { | ||
return &realTimer{ | ||
Timer: time.NewTimer(d), | ||
} | ||
} | ||
|
||
func (c *clockImpl) After(d time.Duration) <-chan time.Time { | ||
return time.After(d) | ||
} | ||
|
||
func (c *clockImpl) AfterFunc(d time.Duration, f func()) Timer { | ||
return &realTimer{ | ||
Timer: time.AfterFunc(d, f), | ||
} | ||
} | ||
|
||
func (c *clockImpl) Ticker(d time.Duration) Ticker { | ||
return &realTicker{ | ||
Ticker: time.NewTicker(d), | ||
} | ||
} | ||
|
||
func (c *clockImpl) WithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc) { | ||
return context.WithDeadline(parent, deadline) | ||
} | ||
|
||
func (c *clockImpl) WithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(parent, timeout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clock | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
const ( | ||
clockKey contextKeyType = "Clock" | ||
) | ||
|
||
type contextKeyType string | ||
|
||
var clock = new(clockImpl) | ||
|
||
// WithClock wraps parent in a new context with Clock | ||
func WithClock(parent context.Context, clock Clock) context.Context { | ||
if parent == nil { | ||
panic("cannot create context from nil parent") | ||
} | ||
return context.WithValue(parent, clockKey, clock) | ||
} | ||
|
||
// FromContext returns Clock from context | ||
func FromContext(ctx context.Context) Clock { | ||
if rv, ok := ctx.Value(clockKey).(Clock); ok { | ||
return rv | ||
} | ||
return clock | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Ticker is a time.Ticker interface | ||
type Ticker interface { | ||
C() <-chan time.Time | ||
Stop() | ||
Reset(d time.Duration) | ||
} | ||
|
||
type realTicker struct { | ||
*time.Ticker | ||
} | ||
|
||
func (t *realTicker) C() <-chan time.Time { | ||
return t.Ticker.C | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Timer is a time.Timer interface | ||
type Timer interface { | ||
C() <-chan time.Time | ||
Stop() bool | ||
Reset(d time.Duration) bool | ||
} | ||
|
||
type realTimer struct { | ||
*time.Timer | ||
} | ||
|
||
func (t *realTimer) C() <-chan time.Time { | ||
return t.Timer.C | ||
} |
Oops, something went wrong.