1. Thrift
  2. THRIFT-728

Make generated Haskell code an instance of Arbitrary


    • Patch Info:
      Patch Available


      The patch

      • Generates Arbitrary instances for Thrift structs, enums and exceptions
      • Provides Arbitrary instances for GHC's Int64, Data.Map.Map and Data.Set.Set
      • Makes Thrift enums instances of Bounded and improves the Enum instance declaration

      Making a type an instance of Test.QuickCheck.Arbitrary specifies how to generate random instances of the struct. This is useful for testing.

      For example, consider the following simple Thrift declaration:

      enum LogLevel { DEBUG, INFO, WARNING, ERROR }                                         
      typedef i32 UnixTimeStamp                                                             
      struct LogEntry {                                                                     
         1: LogLevel level,                                                                 
         2: UnixTimeStamp timestamp,                                                        
         3: string message                                                                  
      service Logger {                                                                      
         void log(1: LogEntry entry)                                                        

      With the patch, the following program (import statements elided) is a fuzzer for the log service.

      -- ripped from Test.QuickCheck.Gen.sample'                                            
      infexamples ∷  Gen a →  IO [a]                                                        
      infexamples (MkGen m) =                                                               
        do rnd ←  newStdGen                                                                 
           let rnds rnd = rnd1 : rnds rnd2 where (rnd1, rnd2) = split rnd                   
           return [(m r n) | (r, n) ←  rnds rnd `zip` [0,2..] ]                             
      infentries = infexamples (arbitrary ∷  Gen LogEntry)                                  
      main = do entries ←  infentries                                                       
                forM entries logAnEntry                                                     
      logAnEntry entry = do                                                                 
          transport ←  hOpen ("localhost", PortNumber 9090)                                 
          let binProto = BinaryProtocol transport                                           
          let client = (binProto, binProto)                                                 
          (Client.log client) entry                                                         
          tClose transport                                                                  
          (λ(TransportExn s t) →  print s)
      --On systems with Haskell we could just generate a fuzzer like this one.

      In implementing the Arbitrary instances, it was useful to make Thrift enums instances of Bounded and to improve the Enum instance. Specifically, whereas before,

      [DEBUG .. ]

      would throw an exception, now it behaves as expected without an exception.

      I consider the patch incomplete. It's more of a starting point for a discussion at this point than a serious candidate for inclusion. If it is of interest, I'd appreciate some direction on testing it as well as style, and I'd welcome any other comments or thoughts.

      1. arbitrary.patch
        6 kB
        Aran Donohue

        Issue Links


          Aran Donohue created issue -
          Aran Donohue made changes -
          Field Original Value New Value
          Attachment arbitrary.patch [ 12438156 ]
          Aran Donohue made changes -
          Attachment arbitrary.patch [ 12438156 ]
          Aran Donohue made changes -
          Attachment arbitrary.patch [ 12448827 ]
          Roger Meier made changes -
          Link This issue is part of THRIFT-2641 [ THRIFT-2641 ]
          Roger Meier made changes -
          Link This issue is superceded by THRIFT-2641 [ THRIFT-2641 ]
          Roger Meier made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Assignee Roger Meier [ roger.meier ]
          Resolution Fixed [ 1 ]
          Jake Farrell made changes -
          Fix Version/s 0.9.2 [ 12324954 ]
          Jake Farrell made changes -
          Status Resolved [ 5 ] Closed [ 6 ]


            • Assignee:
              Roger Meier
              Aran Donohue
            • Votes:
              0 Vote for this issue
              2 Start watching this issue


              • Created:

                Time Tracking

                Original Estimate - 96h
                Remaining Estimate - 96h
                Time Spent - Not Specified
                Not Specified