@@ -733,3 +733,146 @@ func TestCertificateRevoked(t *testing.T) {
733733 tl .Close ()
734734 cl .Stop ()
735735}
736+
737+ func TestReset (t * testing.T ) {
738+ var c Communicator
739+ conf := config.Configuration {
740+ Servers : []string {"localhost:1234" },
741+ CommunicatorConfig : & clpb.CommunicatorConfig {
742+ MaxPollDelaySeconds : 10 ,
743+ MinFailureDelaySeconds : 10 ,
744+ },
745+ }
746+ dialCalls := make (chan struct {}, 10 )
747+ c .DialContext = func (ctx context.Context , network , addr string ) (net.Conn , error ) {
748+ dialCalls <- struct {}{}
749+ <- ctx .Done ()
750+ return nil , ctx .Err ()
751+ }
752+
753+ cl , err := client .New (
754+ conf ,
755+ client.Components {
756+ Communicator : & c ,
757+ })
758+ if err != nil {
759+ t .Fatalf ("unable to create client: %v" , err )
760+ }
761+ defer cl .Stop ()
762+
763+ // Wait for first dial attempt
764+ select {
765+ case <- dialCalls :
766+ case <- time .After (5 * time .Second ):
767+ t .Fatal ("Timed out waiting for first dial attempt" )
768+ }
769+
770+ // Call Reset
771+ time .Sleep (100 * time .Millisecond )
772+ c .Reset ()
773+
774+ // Wait for second dial attempt (should be immediate, not waiting 10s)
775+ select {
776+ case <- dialCalls :
777+ case <- time .After (2 * time .Second ):
778+ t .Fatal ("Timed out waiting for second dial attempt after Reset" )
779+ }
780+ }
781+
782+ func TestFlush (t * testing.T ) {
783+ // Create a local https server for the client to talk to.
784+ pemCert , pemKey , err := common_util .ServerCert ()
785+ if err != nil {
786+ t .Fatal (err )
787+ }
788+ cp , err := tls .X509KeyPair (pemCert , pemKey )
789+ if err != nil {
790+ t .Fatal (err )
791+ }
792+ ad , err := net .ResolveTCPAddr ("tcp" , "localhost:0" )
793+ if err != nil {
794+ t .Fatal (err )
795+ }
796+ tl , err := net .ListenTCP ("tcp" , ad )
797+ if err != nil {
798+ t .Fatal (err )
799+ }
800+ addr := tl .Addr ().String ()
801+
802+ pollCount := int32 (0 )
803+ mux := http .NewServeMux ()
804+ mux .HandleFunc ("/message" , func (res http.ResponseWriter , req * http.Request ) {
805+ atomic .AddInt32 (& pollCount , 1 )
806+ cd := fspb.ContactData {
807+ SequencingNonce : 42 ,
808+ }
809+ buf , err := proto .Marshal (& cd )
810+ if err != nil {
811+ t .Fatalf ("unable to marshal ContactData: %v" , err )
812+ }
813+ res .Header ().Set ("Content-Type" , "application/octet-stream" )
814+ res .WriteHeader (http .StatusOK )
815+ res .Write (buf )
816+ })
817+
818+ server := http.Server {
819+ Addr : addr ,
820+ Handler : mux ,
821+ TLSConfig : & tls.Config {
822+ ClientAuth : tls .RequireAnyClientCert ,
823+ Certificates : []tls.Certificate {cp },
824+ NextProtos : []string {"h2" },
825+ },
826+ }
827+ l := tls .NewListener (tl , server .TLSConfig )
828+ go server .Serve (l )
829+ defer tl .Close ()
830+
831+ var c Communicator
832+ conf := config.Configuration {
833+ TrustedCerts : x509 .NewCertPool (),
834+ Servers : []string {addr },
835+ CommunicatorConfig : & clpb.CommunicatorConfig {
836+ MaxPollDelaySeconds : 10 ,
837+ MinFailureDelaySeconds : 10 ,
838+ },
839+ }
840+ if ! conf .TrustedCerts .AppendCertsFromPEM (pemCert ) {
841+ t .Fatal ("unable to add server cert to pool" )
842+ }
843+
844+ cl , err := client .New (
845+ conf ,
846+ client.Components {
847+ Communicator : & c ,
848+ })
849+ if err != nil {
850+ t .Fatalf ("unable to create client: %v" , err )
851+ }
852+ defer cl .Stop ()
853+
854+ // Wait for first poll
855+ for atomic .LoadInt32 (& pollCount ) == 0 {
856+ time .Sleep (100 * time .Millisecond )
857+ }
858+
859+ // Call Flush in a goroutine
860+ flushDone := make (chan error , 1 )
861+ go func () {
862+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
863+ defer cancel ()
864+ flushDone <- c .Flush (ctx )
865+ }()
866+
867+ // Trigger a poll via wakeUp (indirectly via Reset)
868+ c .Reset ()
869+
870+ select {
871+ case err := <- flushDone :
872+ if err != nil {
873+ t .Errorf ("Flush failed: %v" , err )
874+ }
875+ case <- time .After (6 * time .Second ):
876+ t .Fatal ("Timed out waiting for Flush" )
877+ }
878+ }
0 commit comments