-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a dedicated store for test results
- Loading branch information
1 parent
02c6c81
commit 5619626
Showing
3 changed files
with
55 additions
and
22 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,45 @@ | ||
package core | ||
|
||
import "sync" | ||
|
||
type Store struct { | ||
results sync.Map | ||
} | ||
|
||
func NewStore() Store { | ||
syncMap := sync.Map{} | ||
|
||
return Store{ | ||
results: syncMap, | ||
} | ||
} | ||
|
||
func (s *Store) AddOrUpdate(res TestResult) { | ||
if res.InProgress { | ||
existing, ok := s.results.Load(res.Id) | ||
if !ok { | ||
s.results.Store(res.Id, res) | ||
} else { | ||
prev := existing.(TestResult) | ||
s.results.Store(res.Id, TestResult{ | ||
Id: prev.Id, | ||
InProgress: true, | ||
Tcp: prev.Tcp, | ||
HttpStatus: prev.HttpStatus, | ||
Duration: prev.Duration, | ||
}) | ||
} | ||
} else { | ||
s.results.Store(res.Id, res) | ||
} | ||
} | ||
|
||
func (s *Store) Clear() { | ||
s.results = sync.Map{} | ||
} | ||
|
||
func (s *Store) ForEach(f func(TestResult) bool) { | ||
s.results.Range(func(key, value any) bool { | ||
return f(value.(TestResult)) | ||
}) | ||
} |