Skip to content

Conversation

PetrGuan
Copy link
Contributor

Add thread safety unit test for LazyCache

Add thread safety unit test for LazyCache
@PetrGuan
Copy link
Contributor Author

PetrGuan commented May 31, 2022

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

@tomerd
Copy link
Contributor

tomerd commented Jun 14, 2022

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
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
Contributor 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
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
Contributor 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