Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Conversation

haacked
Copy link
Contributor

@haacked haacked commented Aug 5, 2015

Added unit tests of the SimpleApiClient and did some light code cleanup.

haacked added 5 commits August 5, 2015 15:21
Want to start adding test coverage so others feel safe making changes.
It's false if it's not OK so no need for this other check.
In theory, if "ClearFormCache" is called after we check `contains` but
before we execute the `return` line, we could get an exception here.

If we're concerned about performance here, we could consider switching
to the ConcurrentDictionary.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the above block because we were accessing cache outside of the lock here. In theory, if ClearFormCache is called after we check contains (line 32) but before we execute the return (line 33), we could get an exception here because the item could be removed from the cache.

If we're concerned about performance here, we could consider switching to the ConcurrentDictionary, but I'm not sure this is a hot spot. Thoughts @shana?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm almost certain I did this because Create will never get called at the same time as ClearFromCache, and the lock in Create is not to protect Create from ClearFromCache, but to have only the first instance that comes into Create trigger the creation, and keep everyone else from never even reaching the lock.

When the team explorer home page loads, there's a series of requests that get triggered to go through Create, all at the same time - one for each section and navigation item. If there's already an api client, great, return that, but if there isn't, whoever gets to the lock first gets to create it. Since the section gets created first and there's a slight delay between it and the nav items being created, that means that potentially the nav items never even hit the lock and just return immediately.

I need to confirm this, and maybe change the names on these locks to be more clear about what their role is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like ConcurrentDictionary would be useful for this then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it might be. I'm looking at it now, and ConcurrentDictionary has this interesting remark on its MSDN page (https://msdn.microsoft.com/en-us/library/ee378677(v=vs.110).aspx):

If you call GetOrAdd simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the dictionary for every call.

That means that it can potentially create multiple instances of SimpleApiClient and then discard them, returning only the first one created (or the first one inserted, whichever comes, erm, first). Given that the SimpleApiClient ctor doesn't do anything, that should be fine.

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'm fine with that behavior because creating them is cheap. Especially if it's discarded. Locks are less cheap.

I put shit in the wrong place.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, these were awfully silly bits of code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😛

@shana
Copy link
Contributor

shana commented Aug 19, 2015

Added the ConcurrentDictionary implementation! Everything else looks ✨

shana added a commit that referenced this pull request Aug 19, 2015
@shana shana merged commit 8096a8e into master Aug 19, 2015
@shana shana deleted the haacked/add-unit-tests branch August 19, 2015 11:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants