Philip Zampino created KNOX-3042: ------------------------------------ Summary: 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
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)