Skip to content

Add thread safety unit test for LazyCache#318

Open
PetrGuan wants to merge 3 commits into
swiftlang:mainfrom
PetrGuan:patch-1
Open

Add thread safety unit test for LazyCache#318
PetrGuan wants to merge 3 commits into
swiftlang:mainfrom
PetrGuan:patch-1

Conversation

@PetrGuan

Copy link
Copy Markdown

Add thread safety unit test for LazyCache

Add thread safety unit test for LazyCache
@PetrGuan

PetrGuan commented May 31, 2022

Copy link
Copy Markdown
Author

@compnerd @tomerd Can someone take a look? Thanks!

@tomerd

tomerd commented Jun 14, 2022

Copy link
Copy Markdown
Contributor

thanks @PetrGuan can you elaborate what this is testing?

var bar: Int { return barCache.getValue(self) }
var barCache = LazyCache<Foo, Int>(someExpensiveMethod)
func someExpensiveMethod() -> Int {
numCalls += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this thread safe?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think so. The someExpensiveMethod() is protected by a lock. Maybe LazyCache should be named something like ThreadSafeLazyCache?

let foo = Foo()
for _ in 0..<10 {
dispatchGroup.enter()
DispatchQueue.global().async {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass the group to the queue method instead of manually enter/leave

@PetrGuan

Copy link
Copy Markdown
Author

thanks @PetrGuan can you elaborate what this is testing?

@tomerd Thanks for reviewing! I think it's testing the ThreadSafety for the LazyCache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants