-
Notifications
You must be signed in to change notification settings - Fork 783
Fix cache hash for user-defined record types #7184
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
Closed
+132
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,9 +22,28 @@ import java.nio.file.Paths | |
| import com.google.common.hash.Hashing | ||
| import nextflow.Global | ||
| import nextflow.Session | ||
| import nextflow.script.types.Record | ||
| import org.apache.commons.codec.digest.DigestUtils | ||
| import spock.lang.Specification | ||
| import test.TestHelper | ||
|
|
||
| class SampleRecord implements Record { | ||
| public String sample | ||
| public Integer count | ||
| SampleRecord(String sample, Integer count) { | ||
| this.sample = sample | ||
| this.count = count | ||
| } | ||
| } | ||
|
|
||
| class NestedRecord implements Record { | ||
| public String id | ||
| public SampleRecord inner | ||
| NestedRecord(String id, SampleRecord inner) { | ||
| this.id = id | ||
| this.inner = inner | ||
| } | ||
| } | ||
| /** | ||
| * | ||
| * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
|
|
@@ -154,6 +173,78 @@ class HashBuilderTest extends Specification { | |
| hash1.hash() == hash2.hash() | ||
| } | ||
|
|
||
| def 'should hash typed record by field values, independent of JVM identity'() { | ||
| given: | ||
| def r1 = new SampleRecord('alpha', 1) | ||
| def r2 = new SampleRecord('alpha', 1) | ||
| def r3 = new SampleRecord('beta', 1) | ||
| def r4 = new SampleRecord('alpha', 2) | ||
|
Comment on lines
+178
to
+181
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Records cannot be created via constructor. But it may be that this happens to work and is only disallowed by the type checker |
||
|
|
||
| expect: 'records with same field values hash to the same value' | ||
| CacheHelper.hasher(r1).hash() == CacheHelper.hasher(r2).hash() | ||
|
|
||
| and: 'records with different field values hash differently' | ||
| CacheHelper.hasher(r1).hash() != CacheHelper.hasher(r3).hash() | ||
| CacheHelper.hasher(r1).hash() != CacheHelper.hasher(r4).hash() | ||
| } | ||
|
|
||
| def 'typed record and equivalent RecordMap should hash to the same value'() { | ||
| given: | ||
| def typed = new SampleRecord('alpha', 1) | ||
| def asMap = new RecordMap([sample: 'alpha', count: 1]) | ||
|
|
||
| expect: | ||
| CacheHelper.hasher(typed).hash() == CacheHelper.hasher(asMap).hash() | ||
| } | ||
|
|
||
| def 'record built with the record(...) built-in should hash the same as the typed constructor'() { | ||
| given: 'a record built with the stdlib record(...) helper' | ||
| def builtin = nextflow.Nextflow.record(sample: 'alpha', count: 1) | ||
|
|
||
| and: 'an equivalent record built with the typed-record constructor' | ||
| def typed = new SampleRecord('alpha', 1) | ||
|
|
||
| expect: | ||
| CacheHelper.hasher(builtin).hash() == CacheHelper.hasher(typed).hash() | ||
| } | ||
|
|
||
| def 'record(...) built-in hash should not depend on the order of named arguments'() { | ||
| given: | ||
| def r1 = nextflow.Nextflow.record(sample: 'alpha', count: 1) | ||
| def r2 = nextflow.Nextflow.record(count: 1, sample: 'alpha') | ||
|
|
||
| expect: | ||
| CacheHelper.hasher(r1).hash() == CacheHelper.hasher(r2).hash() | ||
| } | ||
|
|
||
| def 'nested typed records should hash by value'() { | ||
| given: | ||
| def n1 = new NestedRecord('x', new SampleRecord('alpha', 1)) | ||
| def n2 = new NestedRecord('x', new SampleRecord('alpha', 1)) | ||
| def n3 = new NestedRecord('x', new SampleRecord('beta', 1)) | ||
| def n4 = new NestedRecord('y', new SampleRecord('alpha', 1)) | ||
|
|
||
| expect: 'same outer and same inner field values hash equally' | ||
| CacheHelper.hasher(n1).hash() == CacheHelper.hasher(n2).hash() | ||
|
|
||
| and: 'a change to the inner record invalidates the outer hash' | ||
| CacheHelper.hasher(n1).hash() != CacheHelper.hasher(n3).hash() | ||
|
|
||
| and: 'a change to the outer field invalidates the hash' | ||
| CacheHelper.hasher(n1).hash() != CacheHelper.hasher(n4).hash() | ||
| } | ||
|
|
||
| def 'nested typed record and nested RecordMap should hash equally'() { | ||
| given: | ||
| def typed = new NestedRecord('x', new SampleRecord('alpha', 1)) | ||
| def asMap = nextflow.Nextflow.record( | ||
| id: 'x', | ||
| inner: nextflow.Nextflow.record(sample: 'alpha', count: 1) ) | ||
|
|
||
| expect: | ||
| CacheHelper.hasher(typed).hash() == CacheHelper.hasher(asMap).hash() | ||
| } | ||
|
|
||
| def 'directories with same content but different structure should yield different hashes'() { | ||
| given: | ||
| def folder = TestHelper.createInMemTempDir() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every record instance is a RecordMap, so it should be caught by the
Mapcase above