{"record":{"id":"491d9e9e55337722","repo":"apache/kafka","slug":"you-can-only-check-the-position-for-partitions-ass","errorCode":null,"errorMessage":"You can only check the position for partitions assigned to this consumer.","messagePattern":"You can only check the position for partitions assigned to this consumer\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":489,"sourceCode":"    public synchronized Map<TopicPartition, OffsetAndMetadata> committed(final Set<TopicPartition> partitions) {\n        ensureNotClosed();\n\n        return partitions.stream()\n            .filter(committed::containsKey)\n            .collect(Collectors.toMap(tp -> tp, tp -> subscriptions.isAssigned(tp) ?\n                committed.get(tp) : new OffsetAndMetadata(0)));\n    }\n\n    @Override\n    public synchronized Map<TopicPartition, OffsetAndMetadata> committed(final Set<TopicPartition> partitions, final Duration timeout) {\n        return committed(partitions);\n    }\n\n    @Override\n    public synchronized long position(TopicPartition partition) {\n        ensureNotClosed();\n        if (!this.subscriptions.isAssigned(partition))\n            throw new IllegalArgumentException(\"You can only check the position for partitions assigned to this consumer.\");\n        SubscriptionState.FetchPosition position = this.subscriptions.position(partition);\n        if (position == null) {\n            updateFetchPosition(partition);\n            position = this.subscriptions.position(partition);\n        }\n        return position.offset;\n    }\n\n    @Override\n    public synchronized long position(TopicPartition partition, final Duration timeout) {\n        return position(partition);\n    }\n\n    @Override\n    public synchronized void seekToBeginning(Collection<TopicPartition> partitions) {\n        ensureNotClosed();\n        subscriptions.requestOffsetReset(partitions, AutoOffsetResetStrategy.EARLIEST);\n    }","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L471-L507","documentation":"Thrown by MockConsumer.position(TopicPartition) when the partition is not in the consumer's current assignment. Position only has meaning for partitions the consumer is actively consuming; asking for a position on an unassigned partition is a programming error in the test. The guard mirrors the real consumer's behavior.","triggerScenarios":"Calling position(tp) before assign(); tp with wrong topic name or partition number; calling position after revoke but before re-assign.","commonSituations":"Test setup ordering bug (position before assign); stale TopicPartition references after a simulated rebalance; refactor that changed partition counts.","solutions":["Ensure the TopicPartition is in mock.assignment() before calling position().","Call assign() or simulate rebalance so the partition is assigned.","Derive tp from the same constant used during assign to avoid drift."],"exampleFix":"// before\nTopicPartition tp = new TopicPartition(\"orders\", 0);\nlong pos = mockConsumer.position(tp); // not assigned\n\n// after\nmockConsumer.assign(Set.of(tp));\nmockConsumer.updateBeginningOffsets(Map.of(tp, 0L));\nlong pos = mockConsumer.position(tp);","handlingStrategy":"validation","validationCode":"if (!mockConsumer.assignment().contains(tp))\n    throw new IllegalStateException(\"position() requires \" + tp + \" to be assigned\");\nreturn mockConsumer.position(tp);","typeGuard":"static boolean canQueryPosition(MockConsumer<?,?> m, TopicPartition tp) {\n    return m.assignment().contains(tp);\n}","tryCatchPattern":"try {\n    return mockConsumer.position(tp);\n} catch (IllegalArgumentException e) {\n    if (\"You can only check the position for partitions assigned to this consumer.\".equals(e.getMessage())) {\n        mockConsumer.assign(union(mockConsumer.assignment(), Set.of(tp)));\n        return mockConsumer.position(tp);\n    }\n    throw e;\n}","preventionTips":["Call assign() before position() in tests.","Use a helper that asserts the partition is assigned before reading its position.","Recompute TopicPartition references from shared constants."],"tags":["consumer","mock-consumer","test","illegal-state","offsets"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}