Skip to content

Commit

Permalink
Support for io.EOF to C.AVERROR_EOF mapping (#73)
Browse files Browse the repository at this point in the history
* Support for io.EOF to C.AVERROR_EOF mapping

* Requested changes
  • Loading branch information
daniel-sullivan authored and asticode committed Oct 23, 2024
1 parent e1f7d0c commit 716eb5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
3 changes: 3 additions & 0 deletions io_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "C"
import (
"errors"
"fmt"
"io"
"sync"
"unsafe"
)
Expand Down Expand Up @@ -241,6 +242,8 @@ func goAstiavIOContextReadFunc(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.
var e Error
if errors.As(err, &e) {
return C.int(e)
} else if errors.Is(err, io.EOF) {
return C.AVERROR_EOF
}
return C.AVERROR_UNKNOWN
}
Expand Down
69 changes: 42 additions & 27 deletions io_context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astiav

import (
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -9,34 +10,48 @@ import (
)

func TestIOContext(t *testing.T) {
var seeked bool
rb := []byte("read")
wb := []byte("write")
var written []byte
c, err := AllocIOContext(8, true, func(b []byte) (int, error) {
copy(b, rb)
return len(rb), nil
}, func(offset int64, whence int) (n int64, err error) {
seeked = true
return offset, nil
}, func(b []byte) (int, error) {
written = make([]byte, len(b))
copy(written, b)
return len(b), nil
t.Run("read write seek", func(t *testing.T) {
var seeked bool
rb := []byte("read")
wb := []byte("write")
var written []byte
c, err := AllocIOContext(8, true, func(b []byte) (int, error) {
copy(b, rb)
return len(rb), nil
}, func(offset int64, whence int) (n int64, err error) {
seeked = true
return offset, nil
}, func(b []byte) (int, error) {
written = make([]byte, len(b))
copy(written, b)
return len(b), nil
})
require.NoError(t, err)
defer c.Free()
b := make([]byte, 6)
n, err := c.Read(b)
require.NoError(t, err)
require.Equal(t, 4, n)
require.Equal(t, rb, b[:n])
_, err = c.Seek(2, 0)
require.NoError(t, err)
require.True(t, seeked)
c.Write(wb)
c.Flush()
require.Equal(t, wb, written)
})

t.Run("io.EOF is mapped to AVERROR_EOF when reading", func(t *testing.T) {
c, err := AllocIOContext(8, false, func(b []byte) (int, error) {
return 0, io.EOF
}, nil, nil)
require.NoError(t, err)
defer c.Free()
b := make([]byte, 100)
n, err := c.Read(b)
require.ErrorIs(t, err, ErrEof)
require.Equal(t, 0, n)
})
require.NoError(t, err)
defer c.Free()
b := make([]byte, 6)
n, err := c.Read(b)
require.NoError(t, err)
require.Equal(t, 4, n)
require.Equal(t, rb, b[:n])
_, err = c.Seek(2, 0)
require.NoError(t, err)
require.True(t, seeked)
c.Write(wb)
c.Flush()
require.Equal(t, wb, written)
}

func TestOpenIOContext(t *testing.T) {
Expand Down

0 comments on commit 716eb5d

Please sign in to comment.