@Test public void testSendWhenBufferIsFullInDeveloperMode() throws Exception { TelemetriesTransmitter mockSender = Mockito.mock(TelemetriesTransmitter.class); LimitsEnforcer maxEnforcer = createEnforcerWithCurrentValue(1); LimitsEnforcer sendEnforcer = createDefaultSenderTimeoutEnforcer(); TelemetryBuffer testedBuffer = new TelemetryBuffer(mockSender, maxEnforcer, sendEnforcer); for (int i = 0; i < 2; ++i) { testedBuffer.add("mockTelemetry"); } Mockito.verify(mockSender, Mockito.never()).scheduleSend((TelemetriesTransmitter.TelemetriesFetcher)any(), anyLong(), (TimeUnit)anyObject()); Mockito.verify(mockSender, Mockito.times(2)).sendNow(anyCollectionOf(String.class)); }
public void add(T telemetry) { Preconditions.checkNotNull(telemetry, "Telemetry must be non null value"); synchronized (lock) { telemetries.add(telemetry); int currentSize = telemetries.size(); if (currentSize >= maxTelemetriesInBatch) { if (!sender.sendNow(prepareTelemetriesForSend())) { logger.error("Failed to send buffer data to network"); } } else if (currentSize == 1) { if (!sender.scheduleSend(new TelemetryBufferTelemetriesFetcher(generation), transmitBufferTimeoutInSeconds, TimeUnit.SECONDS)) { logger.error("Failed to schedule send of the buffer to network"); telemetries.clear(); } } } }
TelemetryBuffer { public void add(T telemetry) { Preconditions.checkNotNull(telemetry, "Telemetry must be non null value"); synchronized (lock) { telemetries.add(telemetry); int currentSize = telemetries.size(); if (currentSize >= maxTelemetriesInBatch) { if (!sender.sendNow(prepareTelemetriesForSend())) { logger.error("Failed to send buffer data to network"); } } else if (currentSize == 1) { if (!sender.scheduleSend(new TelemetryBufferTelemetriesFetcher(generation), transmitBufferTimeoutInSeconds, TimeUnit.SECONDS)) { logger.error("Failed to schedule send of the buffer to network"); telemetries.clear(); } } } } }
TelemetryBuffer { public void add(T telemetry) { Preconditions.checkNotNull(telemetry, "Telemetry must be non null value"); synchronized (lock) { telemetries.add(telemetry); int currentSize = telemetries.size(); if (currentSize >= maxTelemetriesInBatch) { if (!sender.sendNow(prepareTelemetriesForSend())) { logger.error("Failed to send buffer data to network"); } } else if (currentSize == 1) { if (!sender.scheduleSend(new TelemetryBufferTelemetriesFetcher(generation), transmitBufferTimeoutInSeconds, TimeUnit.SECONDS)) { logger.error("Failed to schedule send of the buffer to network"); telemetries.clear(); } } } } TelemetryBuffer(TelemetriesTransmitter<T> sender, LimitsEnforcer maxTelemetriesInBatchEnforcer, LimitsEnforcer transmitBufferTimeoutInSecondsEnforcer); }
TelemetryBuffer { public void add(T telemetry) { Preconditions.checkNotNull(telemetry, "Telemetry must be non null value"); synchronized (lock) { telemetries.add(telemetry); int currentSize = telemetries.size(); if (currentSize >= maxTelemetriesInBatch) { if (!sender.sendNow(prepareTelemetriesForSend())) { logger.error("Failed to send buffer data to network"); } } else if (currentSize == 1) { if (!sender.scheduleSend(new TelemetryBufferTelemetriesFetcher(generation), transmitBufferTimeoutInSeconds, TimeUnit.SECONDS)) { logger.error("Failed to schedule send of the buffer to network"); telemetries.clear(); } } } } TelemetryBuffer(TelemetriesTransmitter<T> sender, LimitsEnforcer maxTelemetriesInBatchEnforcer, LimitsEnforcer transmitBufferTimeoutInSecondsEnforcer); void setMaxTelemetriesInBatch(int value); int getMaxTelemetriesInBatch(); void setTransmitBufferTimeoutInSeconds(int value); int getTransmitBufferTimeoutInSeconds(); void add(T telemetry); void flush(); }
TelemetryBuffer { public void add(T telemetry) { Preconditions.checkNotNull(telemetry, "Telemetry must be non null value"); synchronized (lock) { telemetries.add(telemetry); int currentSize = telemetries.size(); if (currentSize >= maxTelemetriesInBatch) { if (!sender.sendNow(prepareTelemetriesForSend())) { logger.error("Failed to send buffer data to network"); } } else if (currentSize == 1) { if (!sender.scheduleSend(new TelemetryBufferTelemetriesFetcher(generation), transmitBufferTimeoutInSeconds, TimeUnit.SECONDS)) { logger.error("Failed to schedule send of the buffer to network"); telemetries.clear(); } } } } TelemetryBuffer(TelemetriesTransmitter<T> sender, LimitsEnforcer maxTelemetriesInBatchEnforcer, LimitsEnforcer transmitBufferTimeoutInSecondsEnforcer); void setMaxTelemetriesInBatch(int value); int getMaxTelemetriesInBatch(); void setTransmitBufferTimeoutInSeconds(int value); int getTransmitBufferTimeoutInSeconds(); void add(T telemetry); void flush();  }