Skip to content

Commit

Permalink
serde should leave its stream open
Browse files Browse the repository at this point in the history
  • Loading branch information
usix79 committed Jun 13, 2022
1 parent b7e2f4c commit e4c2ab9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ msbuild.wrn
tools/

.fake
.ionide
.ionide

.DS_Store
4 changes: 2 additions & 2 deletions src/Serde.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let binarySerializer (options:SerializationOptions) (type':Type) (schema:Schema)
let factory = TypeFactory(tr, schema, (fun _ _ _ -> null))
let director = InstanceDirector.create options tr schema factory
fun (instance:obj) (stream:Stream) ->
use writer = new BinaryWriter(stream, Text.Encoding.UTF8)
use writer = new BinaryWriter(stream, Text.Encoding.UTF8, true)
let builder = BinaryBuilder(writer)
director instance builder

Expand Down Expand Up @@ -64,6 +64,6 @@ let binaryDeserializer (options:DeserializationOptions) (type':Type) (readerSche
let factory = TypeFactory(tr, readerSchema, createDefaultValue options)
fun (writerSchema: Schema) (stream:Stream) ->
let builder = InstanceBuilder(options, factory)
use reader = new BinaryReader(stream, Text.Encoding.UTF8)
use reader = new BinaryReader(stream, Text.Encoding.UTF8, true)
director reader writerSchema builder
builder.Instance
28 changes: 27 additions & 1 deletion test/SerdeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,30 @@ let binary2SimpleCasesTests =
let binary2EvolutionTests =
evolutionCases
|> List.map binary2EvolutionTest
|> testList "Binary2Evolution"
|> testList "Binary2Evolution"

[<Tests>]
let leaveStreamOpenTests =
test "Serde should leave its stream open" {
let subject = "Hello World!"
let subjectType = typeof<string>
let schema =
match Schema.generate Schema.defaultOptions subjectType with
| Ok schema -> schema
| Error err -> failwithf "Schema error: %A" err

let serializer = Serde.binarySerializer Serde.defaultSerializerOptions subjectType schema
let deserializer = Serde.binaryDeserializer Serde.defaultDeserializerOptions subjectType schema

use stream = new MemoryStream()
serializer subject stream
stream.Seek(0L, SeekOrigin.Begin) |> ignore
let copy = deserializer schema stream :?> string
Expect.equal "Deserialized message should be equal to the original" subject copy

stream.Seek(0L, SeekOrigin.Begin) |> ignore
serializer subject stream
stream.Seek(0L, SeekOrigin.Begin) |> ignore
let copy2 = deserializer schema stream :?> string
Expect.equal "Deserialized message should be equal to the original" subject copy2
}

0 comments on commit e4c2ab9

Please sign in to comment.