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

ResourceSet will only iterate its elements once #684

Open
rjbird77 opened this issue May 20, 2022 · 2 comments
Open

ResourceSet will only iterate its elements once #684

rjbird77 opened this issue May 20, 2022 · 2 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@rjbird77
Copy link

Issue Summary

ResourceSet implement Iterable, but its implementation is flawed. You can only iterate through its elements once, even if you get a new iterator.

ResourceSet implements Iterable and has inner class ResourceSetIterator for its iterator internally. The implementation of ResourceSetIterator uses class variables from ResourceSet, specifically ResourceSet.iterator and ResourceSet.processed. Because of this, even though ResourceSet.iterator() method does create a new instance of ResourceSetIterator, since the processed count and the iterator in the outer class never get reset, it is functionally still at the end (hasNext() method returns false).

Steps to Reproduce

  1. Get a ResourceSet
  2. Iterate through the elements - works
  3. Iterate through the elements again - never goes into the loop

Code Snippet

import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.monitor.v1.Alert;

public class Example {
	public static void main(String[] args) {
		Twilio.init("my_account_sid", "my_auth_token");
		ResourceSet<Alert> alerts = Alert.reader().limit(5).read();

		System.out.println("Before first loop:");
		for(Alert record : alerts) {
			System.out.println("     First loop - " + record.getSid());
		}

		System.out.println("Before second loop:");
		for(Alert record : alerts) {
			System.out.println("     Second loop - " + record.getSid());
		}
		System.out.println("After second loop");
	}
}

Exception/Log

Output from above code - second loop had no output:

Before first loop:
     First loop - NO06bc5d5591ecd607e21f5f3436dc22b0
     First loop - NO48d74ff1f40c2ef88d4c74dbc0d39907
     First loop - NO27183f081d5903afb84ec8b5e3a1b7be
     First loop - NO8426237981e0ea63bab859157c1c9ca8
     First loop - NOc2ef3b0918b79d2a62a0efc8290a1885
Before second loop:
After second loop

Technical details:

  • twilio-java version: 8.30.1
  • java version: openjdk version "1.8.0_322"
@claudiachua claudiachua added type: bug bug in the library status: help wanted requesting help from the community labels Jun 1, 2022
@claudiachua
Copy link

Hi @rjbird77, this issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@JerinTheElite
Copy link

JerinTheElite commented Mar 7, 2024

The same issue in the latest Twilio Java SDK 10.1.0 as well. I'm using Java 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

3 participants