Conversation
cross_platform_lock.go
Outdated
| "github.com/AzureAD/microsoft-authentication-extensions-for-go/flock" | ||
| ) | ||
|
|
||
| type CrossPlatLock struct { |
There was a problem hiding this comment.
type Lock will be better
Lock.New() for initializing Lock
Directory structure - extensions main directory and then lock directory
cross_platform_lock.go
Outdated
| ) | ||
|
|
||
| type CrossPlatLock struct { | ||
| retryNumber int |
cross_platform_lock.go
Outdated
|
|
||
| type CrossPlatLock struct { | ||
| retryNumber int | ||
| retryDelayMilliseconds time.Duration |
cross_platform_lock.go
Outdated
|
|
||
| lockFile *os.File | ||
|
|
||
| fileLock *flock.Flock |
cross_platform_lock.go
Outdated
|
|
||
| fileLock *flock.Flock | ||
|
|
||
| lockfileName string |
cross_platform_lock.go
Outdated
| fileLock *flock.Flock | ||
|
|
||
| lockfileName string | ||
| locked bool |
cross_platform_lock.go
Outdated
|
|
||
| lockfileName string | ||
| locked bool | ||
| } |
There was a problem hiding this comment.
Add internal sync.Mutex called mu.
cross_platform_lock.go
Outdated
| } | ||
|
|
||
| func (c CrossPlatLock) Lock() error { | ||
| for tryCount := 0; tryCount < c.retryNumber; tryCount++ { |
There was a problem hiding this comment.
Add these lines:
c.mu.Lock()
defer c.mu.Unlock()
Change (c CrossPlatLock) to use a pointer
cross_platform_lock.go
Outdated
| locked bool | ||
| } | ||
|
|
||
| func NewLock(lockFileName string, retryNumber int, retryDelayMilliseconds time.Duration) (CrossPlatLock, error) { |
There was a problem hiding this comment.
Have this return a pointer
There was a problem hiding this comment.
Let's get rid of lockFileName, use a default defined in a constructor.
Let's use this signature:
type Option func(l *Lock)
func WithRetries(n int) Option{
return func(l *Lock) {
l.retries = n
}
}
func New(options ...Option) (*Lock, error) {
l := &Lock{
...
}
for _, o := range options {
o(l)
}
...
return l, nil
}
cross_platform_lock.go
Outdated
| return errors.New("Failed to acquire lock") | ||
| } | ||
|
|
||
| func (c CrossPlatLock) UnLock() error { |
cross_platform_lock.go
Outdated
| return nil | ||
| } | ||
| } | ||
| return errors.New("Failed to acquire lock") |
There was a problem hiding this comment.
error text must start with lower case letter
extensions/lock/lock_test.go
Outdated
|
|
||
| func validateResult(cacheFile string, t *testing.T) int { | ||
| count := 0 | ||
| var prevProc string = "" |
There was a problem hiding this comment.
var
(
count int
prevProc, tag, proc string
)
)
extensions/lock/lock_test.go
Outdated
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
| dat := string(data) |
There was a problem hiding this comment.
All in one line and call it s instead of ele
| } | ||
|
|
||
| func New(lockFileName string, options ...Option) (*Lock, error) { | ||
| l := &Lock{} |
There was a problem hiding this comment.
You need to add default value for retries and retryDelay.
| l.retries = n | ||
| } | ||
| } | ||
| func WithRetryDelay(t time.Duration) Option { |
There was a problem hiding this comment.
Public requires a comment. I'd put:
// WithRetryDelay changes the default delay from [delay] to t.
|
|
||
| type Option func(l *Lock) | ||
|
|
||
| func WithRetries(n int) Option { |
There was a problem hiding this comment.
Public requires a comment. I'd put:
// WithRetries changes the default number of retries from [retries] to n. Negative values panic()
Also, add panic on negative values.
| } | ||
| } | ||
|
|
||
| func New(lockFileName string, options ...Option) (*Lock, error) { |
There was a problem hiding this comment.
lockFileName can be just "p".
// New creates a Lock for a lock file at "p". If "p" doesn't exist, it will be created when using. Lock().
func New(p string options ...Option) (*Lock, error)
| func (l *Lock) Lock() error { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
| for tryCount := 0; tryCount < l.retries; tryCount++ { |
There was a problem hiding this comment.
for i := 0; i < l.retries; i++ {
...
| prevProc = "" | ||
| } | ||
| return count, nil | ||
| } |
There was a problem hiding this comment.
Add two tests:
One tests all the conditions of Lock().
Another tests all the conditions of Unlock()
- flock.Unlock() has error
- Lockfile can't be removed
You can do this by providing an interface:
type flocker interface{
TryLock() error
Unlock() error
Path() string
}
type fakeFlock struct {
err bool
}
func (f fakeFlock) TryLock() error {
if f.err {
return errors.New("error")
}
return nil
}
func (f fakeFlock) Unlock() error {
if f.err {
return errors.New("error")
}
return nil
}
| const cacheFile = "cache.txt" | ||
|
|
||
| func TestLocking(t *testing.T) { | ||
|
|
| if strings.TrimSpace(s) == "" { | ||
| continue | ||
| } | ||
| count += 1 |
| require ( | ||
| github.com/AzureAD/microsoft-authentication-library-for-go v0.3.0 | ||
| github.com/billgraziano/dpapi v0.4.0 | ||
| github.com/gofrs/flock v0.8.1 |
There was a problem hiding this comment.
This flock line shouldn't. be here. So either a path change is needed or you need to run go tidy
| @@ -0,0 +1,50 @@ | |||
| package internal | |||
There was a problem hiding this comment.
Move these files under:
internal/accessor
accessor.go
file/
file.go
windows/
window.go
accessor.go:
- Defines type Cache interface{}
file.go: - Defines Accessor struct{}
- Defines New() constructor
windows.go - Defines Accessor struct{}
- Defines New() constructor
All "Accessor" types implement accessor.Cache
No description provided.