[ https://issues.apache.org/jira/browse/KNOX-3042?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18007755#comment-18007755 ]
ASF subversion and git services commented on KNOX-3042: ------------------------------------------------------- Commit 66eb49549da28a23dd98bd69d69d8e3b5ddd0ed0 in knox's branch refs/heads/dependabot/maven/com.nimbusds-nimbus-jose-jwt-10.0.2 from bonampak [ https://gitbox.apache.org/repos/asf?p=knox.git;h=66eb49549 ] KNOX-3042 - Correct KnoxToken comparator (#918) > TokenServiceResourceTest.testUnlimitedTokensPerUser intermittently fails > ------------------------------------------------------------------------ > > Key: KNOX-3042 > URL: https://issues.apache.org/jira/browse/KNOX-3042 > Project: Apache Knox > Issue Type: Test > Components: Tests > Affects Versions: 2.0.1 > Reporter: Philip Zampino > Assignee: Philip Zampino > Priority: Major > Fix For: 2.1.0 > > Time Spent: 0.5h > Remaining Estimate: 0h > > just trying with 1000 tokens (plus the test adds 5 SSO tokens as a given): > (original value was 100, sometimes fails, sometimes passes). > > {code:java} > @Test > public void testUnlimitedTokensPerUser() throws Exception { > testLimitingTokensPerUser(-1, 1000); > }{code} > this fails all the time it seems. > assertEquals(tokens.size(), revokeOldestToken ? configuredLimit + > numberOfKnoxSsoCookies : numberOfTokens + numberOfKnoxSsoCookies); > This assert has the expected and actual in the wrong order, but that is not a > big > deal.tokenMetadata.entrySet().stream().filter(filterPredicate).collect(Collectors.toList()) > = \{ArrayList@3784} size = 1005 > tokenMetadata.entrySet().stream().filter(filterPredicate).map(this::createKnoxTokenFromMetadata).collect(Collectors.toCollection(TreeSet::new)) > = \{TreeSet@3797} size = 848 > (This createKnoxTokenFromMetadata() is just a slight refactor of the below > part in the try-catch) > Root cause: > TokenServiceResourceTest has this method: > {code:java} > private Collection<KnoxToken> fetchTokens(String userName, boolean > createdBy){code} > this collects the filtered tokens into a TreeSet<KnoxToken>. > > {code:java} > final Collection<KnoxToken> tokens = new TreeSet<>(); // TODO should be > LinkedList but not a TreeSet ... try { > tokens.add(new KnoxToken(tokenId, getTokenIssueTime(tokenId), > getTokenExpiration(tokenId), getMaxLifetime(tokenId), metadata.getValue())); > } catch (UnknownTokenException e) { // NOP }{code} > But this KnoxToken has a custom compareTo method. > {code:java} > @Override > public int compareTo(KnoxToken other) { > return Long.compare(this.issueTime, other.issueTime); > }{code} > now this TreeSet will only contain 800+ elements instead of 1005, because it > will treat tokens with the same issue time as > duplicates.[https://stackoverflow.com/questions/43845136/equal-elements-and-tree-set] > "If you look at the source of TreeSet you will see that by default it uses a > TreeMap to save data and in the TreeMap class put method it only uses the > comparator or compareTo methods of the objects to check for equality, but > never the equals method"A simple solution is to use a LinkedList instead of a > TreeSet -- This message was sent by Atlassian Jira (v8.20.10#820010)