Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delta sync bug #263

Merged
merged 4 commits into from
Jun 26, 2019
Merged

Fix delta sync bug #263

merged 4 commits into from
Jun 26, 2019

Conversation

royjit
Copy link
Contributor

@royjit royjit commented Jun 6, 2019

Delta sync was using wrong hash to store the data in the local cache. This caused
delta sync to fail intermittently.

Issue #:
#232

Description of changes:
Hash calculation used in delta calculation was wrong and was producing different results on the same input. This was caused due to the unordered representation of swift Dictionary object which we used for creating hash. This PR addresses this issue by sorting the dictionary content and then using it for hash calculation.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Delta sync was using wrong hash to store the data in the local cache. This caused
delta sync to fail intermittently.
@rohandubal rohandubal requested review from palpatim and rohandubal June 6, 2019 23:22
@rohandubal rohandubal added AppSync contribution Community contribution PRs labels Jun 6, 2019
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
royjit and others added 2 commits June 6, 2019 16:34
AWSAppSyncUnitTests/AppSyncSubscriptionWithSyncTests.swift Outdated Show resolved Hide resolved
}

func testWithAllValuesFilled() {
let subscriptionWithSync = AppSyncSubscriptionWithSync<OnUpvotePostSubscription, ListPostsQuery, ListPostsQuery>(appSyncClient: appsyncClient!,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Here and throughout: please start arguments on the next line after the opening parens to fix indent, or reformat the generic parameters to line these up. As it stands, the actual method call and arguments are off the screen :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the indentation, removed the generics


class AppSyncSubscriptionWithSyncTests: XCTestCase {

var appsyncClient: AWSAppSyncClient?
Copy link
Contributor

Choose a reason for hiding this comment

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

You could make this an implicitly unwrapped optional (var appsyncClient: AWSAppSyncClient!) to avoid constantly force unwrapping in the body of your tests.

Reasoning:

  1. You can't do anything meaningful without the client, so a crash immediately fails the tests instead of getting into an unexpected state (as for example if you had an optional chain or a guard that simply exited early without asserting anything)
  2. You have a proper try/catch block in setUp to catch the setup failures immediately

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

}
}


Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, remove extra lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

@@ -133,6 +133,7 @@
70C68E4D132FE62623DB8C07 /* Pods_AWSAppSyncTestHostApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8C707001F57B091A8A001CAB /* Pods_AWSAppSyncTestHostApp.framework */; };
8032C5415EF414C038394D69 /* Pods_AWSAppSyncTestCommon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 74071C397A83DEA980BB2F4C /* Pods_AWSAppSyncTestCommon.framework */; };
A70604C0C722923A70C937A1 /* Pods_AWSAppSyncTestApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 57F5A94352E1ABE35159489D /* Pods_AWSAppSyncTestApp.framework */; };
B48056BC22A992E200E4F742 /* AppSyncSubscriptionWithSyncTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B48056BB22A992E200E4F742 /* AppSyncSubscriptionWithSyncTests.swift */; };
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: sort the folder by name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorted

### Bug Fixes

* Fix a bug where delta sync was not correctly storing/ retrieving the `lastSyncTime`. See [issue #232](https://github.com/awslabs/aws-mobile-appsync-sdk-ios/issues/232)
* **Breaking API Change** To fix the delta sync logic, there was a change in the hashing function used internally. This change can cause the existing app to ignore the cache for the first sync and fetch data using base query. Subsequent sync operation should work as normal.
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens to any existing cache data? Will it be re-used, overwritten, ignored and left to grow stale?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Existing cache data will be ignored.


let variables = baseQuery.variables?.sorted(by: { $0.0 < $1.0 }).description ?? ""
baseString += type(of: baseQuery).requestString + variables
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, remove trailing whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

if let subscription = subscription {
let variables = subscription.variables?.description ?? ""
baseString = type(of: subscription).requestString + variables
let variables = subscription.variables?.sorted(by: { $0.0 < $1.0 }).description ?? ""
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: spacing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed spacing

}


private class GraphGetQuery: GraphQLQuery {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for this to be namespaced to the test, just extract to a private class in the same file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved outside

XCTAssertEqual(result1, result2, "Hash value should be equal for the two sync operation")

}

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test that ensures that cases that specify the same operation for both baseQuery and deltaQuery work as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

@royjit royjit requested review from palpatim and rohandubal June 11, 2019 19:09
@palpatim palpatim merged commit b2783ba into awslabs:master Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution Community contribution PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants