feat: Ensure StreamMessageConsumer fully encapsulates protobuf for external consumers#104
Conversation
…ternal consumers Closes #102 This change verifies and documents that StreamMessageConsumer + ProtobufMessageParser fully encapsulates protobuf internals, allowing daqifi-desktop to migrate from its direct Google.Protobuf dependency. Changes: - Add ClearBuffer() to IMessageConsumer<T> interface (desktop uses this during reconnection) - Add comprehensive integration tests demonstrating the full pipeline: - Stream → StreamMessageConsumer → ProtobufMessageParser → MessageReceived event Verification Summary: ✓ StreamMessageConsumer reads from any Stream and raises MessageReceived events ✓ ProtobufMessageParser handles malformed data gracefully (byte-skipping, retry) ✓ ErrorOccurred event provides sufficient context for logging (Error, RawData, Timestamp) ✓ Stop() / StopSafely() supports timeout-based cancellation ✓ ClearBuffer() available for WiFi reconnection scenarios ✓ Works with both WiFi (TCP) and Serial streams ✓ MessageReceivedEventArgs<T> provides access without requiring protobuf types Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
Address Qodo PR review suggestions: 1. Replace Thread.Sleep with ManualResetEventSlim in MalformedData test - Uses event-based synchronization instead of arbitrary sleep - Timeout is expected (malformed data produces no events) 2. Improve ClearBuffer test to actually verify buffer clearing - Starts consumer to populate internal buffer with partial data - Verifies QueuedMessageCount is 0 after ClearBuffer() - Confirms consumer remains functional after clear 3. Add new test: ClearBuffer_WhenNotRunning_DoesNotThrow - Verifies ClearBuffer can be called safely on stopped consumer Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
User description
Summary
ClearBuffer()toIMessageConsumer<T>interface for reconnection scenariosCloses #102
Verification Checklist (from issue)
StreamMessageConsumerreads from aStreamand raisesMessageReceivedevents — compatible with desktop's TCP/Serial streamsProtobufMessageParserhandles malformed data gracefully (byte-skipping, retry) — desktop currently relies on catchingInvalidProtocolBufferExceptionto continue; core's parser handles this internallyErrorOccurredevent provides sufficient context for logging (desktop currently logs viaAppLogger.Error)Stop()/StopSafely()supports cancellation (desktop usesCancellationTokenSource)ClearBuffer()is available (desktop calls this during reconnection)MessageReceivedEventArgs<T>provides access to the parsed message without requiring consumers to use protobuf types beyondDaqifiOutMessageitselfIntegration Tests Added
FullPipeline_ValidProtobufMessage_RaisesMessageReceivedEventFullPipeline_MalformedData_HandledGracefullyWithoutExceptionFullPipeline_MultipleMessages_AllDeliveredInOrderFullPipeline_SingleCorruptedByte_ThenValidData_RecoversAndParsesValidMessageFullPipeline_StreamError_ErrorEventProvidesLoggingContextClearBuffer_CalledViaInterface_ClearsInternalBufferStopSafely_CleanShutdown_ReturnsTrueDispose_StopsConsumerAndCleansUpTest plan
🤖 Generated with Claude Code
PR Type
Enhancement, Tests
Description
Add
ClearBuffer()method toIMessageConsumer<T>interface for reconnection scenariosAdd 8 comprehensive integration tests demonstrating full message consumer pipeline
Verify StreamMessageConsumer fully encapsulates protobuf internals for external consumers
Tests cover valid messages, malformed data handling, multiple messages, error scenarios, and resource cleanup
Diagram Walkthrough
File Walkthrough
StreamMessageConsumerIntegrationTests.cs
Comprehensive integration tests for StreamMessageConsumer pipelinesrc/Daqifi.Core.Tests/Communication/Consumers/StreamMessageConsumerIntegrationTests.cs
full message consumer pipeline
multiple message delivery, error recovery, and resource cleanup
length-delimited format and error-throwing test stream
CountdownEvent) to verify asynchronous event delivery
IMessageConsumer.cs
Add ClearBuffer method to IMessageConsumer interfacesrc/Daqifi.Core/Communication/Consumers/IMessageConsumer.cs
ClearBuffer()method to interface for clearing buffered stream andinternal data
may exist