|
53 | 53 | @Config(sdk = Config.ALL_SDKS) // Test all SDKs because network detection logic changed over time.
|
54 | 54 | public final class ExperimentalBandwidthMeterTest {
|
55 | 55 |
|
56 |
| - private static final int SIMULATED_TRANSFER_COUNT = 100; |
57 | 56 | private static final String FAST_COUNTRY_ISO = "TW";
|
58 | 57 | private static final String SLOW_COUNTRY_ISO = "PG";
|
59 | 58 |
|
@@ -666,23 +665,79 @@ public void networkTypeOverride_doesFullReset() {
|
666 | 665 | setActiveNetworkInfo(networkInfoEthernet);
|
667 | 666 | ExperimentalBandwidthMeter bandwidthMeter =
|
668 | 667 | new ExperimentalBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).build();
|
669 |
| - long[] bitrateEstimatesWithNewInstance = simulateTransfers(bandwidthMeter); |
| 668 | + long[] bitrateEstimatesWithNewInstance = |
| 669 | + simulateTransfers(bandwidthMeter, /* simulatedTransferCount= */ 100); |
670 | 670 |
|
671 | 671 | // Create a new instance and seed with some transfers.
|
672 | 672 | setActiveNetworkInfo(networkInfo2g);
|
673 | 673 | bandwidthMeter =
|
674 | 674 | new ExperimentalBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).build();
|
675 |
| - simulateTransfers(bandwidthMeter); |
| 675 | + simulateTransfers(bandwidthMeter, /* simulatedTransferCount= */ 100); |
676 | 676 |
|
677 | 677 | // Override the network type to ethernet and simulate transfers again.
|
678 | 678 | bandwidthMeter.setNetworkTypeOverride(C.NETWORK_TYPE_ETHERNET);
|
679 |
| - long[] bitrateEstimatesAfterReset = simulateTransfers(bandwidthMeter); |
| 679 | + long[] bitrateEstimatesAfterReset = |
| 680 | + simulateTransfers(bandwidthMeter, /* simulatedTransferCount= */ 100); |
680 | 681 |
|
681 | 682 | // If overriding the network type fully reset the bandwidth meter, we expect the bitrate
|
682 | 683 | // estimates generated during simulation to be the same.
|
683 | 684 | assertThat(bitrateEstimatesAfterReset).isEqualTo(bitrateEstimatesWithNewInstance);
|
684 | 685 | }
|
685 | 686 |
|
| 687 | + @Test |
| 688 | + public void getTimeToFirstByteEstimateUs_withSimultaneousTransferEvents_receivesUpdatedValues() { |
| 689 | + ExperimentalBandwidthMeter bandwidthMeter = |
| 690 | + new ExperimentalBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).build(); |
| 691 | + |
| 692 | + Thread thread = |
| 693 | + new Thread("backgroundTransfers") { |
| 694 | + @Override |
| 695 | + public void run() { |
| 696 | + simulateTransfers(bandwidthMeter, /* simulatedTransferCount= */ 10000); |
| 697 | + } |
| 698 | + }; |
| 699 | + thread.start(); |
| 700 | + |
| 701 | + long currentTimeToFirstByteEstimateUs = bandwidthMeter.getTimeToFirstByteEstimateUs(); |
| 702 | + boolean timeToFirstByteEstimateUpdated = false; |
| 703 | + while (thread.isAlive()) { |
| 704 | + long newTimeToFirstByteEstimateUs = bandwidthMeter.getTimeToFirstByteEstimateUs(); |
| 705 | + if (newTimeToFirstByteEstimateUs != currentTimeToFirstByteEstimateUs) { |
| 706 | + currentTimeToFirstByteEstimateUs = newTimeToFirstByteEstimateUs; |
| 707 | + timeToFirstByteEstimateUpdated = true; |
| 708 | + } |
| 709 | + } |
| 710 | + |
| 711 | + assertThat(timeToFirstByteEstimateUpdated).isTrue(); |
| 712 | + } |
| 713 | + |
| 714 | + @Test |
| 715 | + public void getBitrateEstimate_withSimultaneousTransferEvents_receivesUpdatedValues() { |
| 716 | + ExperimentalBandwidthMeter bandwidthMeter = |
| 717 | + new ExperimentalBandwidthMeter.Builder(ApplicationProvider.getApplicationContext()).build(); |
| 718 | + |
| 719 | + Thread thread = |
| 720 | + new Thread("backgroundTransfers") { |
| 721 | + @Override |
| 722 | + public void run() { |
| 723 | + simulateTransfers(bandwidthMeter, /* simulatedTransferCount= */ 10000); |
| 724 | + } |
| 725 | + }; |
| 726 | + thread.start(); |
| 727 | + |
| 728 | + long currentBitrateEstimate = bandwidthMeter.getBitrateEstimate(); |
| 729 | + boolean bitrateEstimateUpdated = false; |
| 730 | + while (thread.isAlive()) { |
| 731 | + long newBitrateEstimate = bandwidthMeter.getBitrateEstimate(); |
| 732 | + if (newBitrateEstimate != currentBitrateEstimate) { |
| 733 | + currentBitrateEstimate = newBitrateEstimate; |
| 734 | + bitrateEstimateUpdated = true; |
| 735 | + } |
| 736 | + } |
| 737 | + |
| 738 | + assertThat(bitrateEstimateUpdated).isTrue(); |
| 739 | + } |
| 740 | + |
686 | 741 | private void setActiveNetworkInfo(NetworkInfo networkInfo) {
|
687 | 742 | setActiveNetworkInfo(networkInfo, TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
|
688 | 743 | }
|
@@ -711,12 +766,13 @@ private void setNetworkCountryIso(String countryIso) {
|
711 | 766 | Shadows.shadowOf(telephonyManager).setNetworkCountryIso(countryIso);
|
712 | 767 | }
|
713 | 768 |
|
714 |
| - private static long[] simulateTransfers(ExperimentalBandwidthMeter bandwidthMeter) { |
715 |
| - long[] bitrateEstimates = new long[SIMULATED_TRANSFER_COUNT]; |
| 769 | + private static long[] simulateTransfers( |
| 770 | + ExperimentalBandwidthMeter bandwidthMeter, int simulatedTransferCount) { |
| 771 | + long[] bitrateEstimates = new long[simulatedTransferCount]; |
716 | 772 | Random random = new Random(/* seed= */ 0);
|
717 | 773 | DataSource dataSource = new FakeDataSource();
|
718 | 774 | DataSpec dataSpec = new DataSpec(Uri.parse("https://test.com"));
|
719 |
| - for (int i = 0; i < SIMULATED_TRANSFER_COUNT; i++) { |
| 775 | + for (int i = 0; i < simulatedTransferCount; i++) { |
720 | 776 | bandwidthMeter.onTransferInitializing(dataSource, dataSpec, /* isNetwork= */ true);
|
721 | 777 | ShadowSystemClock.advanceBy(Duration.ofMillis(random.nextInt(50)));
|
722 | 778 | bandwidthMeter.onTransferStart(dataSource, dataSpec, /* isNetwork= */ true);
|
|
0 commit comments