Session now Conn, and NewSession()+Open() replaced by Dial().

This commit is contained in:
scruzin 2019-01-19 13:25:05 +10:30
parent fc7ae413d3
commit 9cc0e5631c
1 changed files with 31 additions and 55 deletions

View File

@ -116,75 +116,54 @@ func TestKey(t *testing.T) {
testLog(0, "Testing against URL "+testBaseURL+testKey) testLog(0, "Testing against URL "+testBaseURL+testKey)
} }
// TestInit tests session construction and link initialization.
func TestInit(t *testing.T) {
testLog(0, "TestInit")
// test with just the base URL
s := NewSession(testBaseURL, testTimeout, testLog)
if s.url != testBaseURL && s.link.timeout != testTimeout {
t.Errorf("NewSession failed")
}
err := s.init()
if err != nil {
t.Errorf("setupURL: failed with error: %v", err)
}
// test the parts are as expected
if s.link.protocol&featureWrite == 0 {
t.Errorf("setupURL: link not writable")
}
if rtmpProtocolStrings[s.link.protocol&^featureWrite] != rtmpProtocol {
t.Errorf("setupURL: wrong protocol: %v", s.link.protocol)
}
if s.link.host != testHost {
t.Errorf("setupURL: wrong host: %v", s.link.host)
}
if s.link.app != testApp {
t.Errorf("setupURL: wrong app: %v", s.link.app)
}
}
// TestErrorHandling tests error handling // TestErrorHandling tests error handling
func TestErorHandling(t *testing.T) { func TestErorHandling(t *testing.T) {
testLog(0, "TestErrorHandling") testLog(0, "TestErrorHandling")
if testKey == "" { if testKey == "" {
t.Skip("Skipping TestErrorHandling since no RTMP_TEST_KEY") t.Skip("Skipping TestErrorHandling since no RTMP_TEST_KEY")
} }
s := NewSession(testBaseURL+testKey, testTimeout, testLog) c, err := Dial(testBaseURL+testKey, testTimeout, testLog)
if err != nil {
// test errNotConnected t.Errorf("Dial failed with error: %v", err)
var buf [1024]byte
tag := buf[:0]
_, err := s.Write(tag)
if err == nil {
t.Errorf("Write did not return errNotConnected")
} }
err = s.Open() // test the link parts are as expected
if err != nil { if c.link.protocol&featureWrite == 0 {
t.Errorf("Open failed with error: %v", err) t.Errorf("link not writable")
}
if rtmpProtocolStrings[c.link.protocol&^featureWrite] != rtmpProtocol {
t.Errorf("wrong protocol: %v", c.link.protocol)
}
if c.link.host != testHost {
t.Errorf("wrong host: %v", c.link.host)
}
if c.link.app != testApp {
t.Errorf("wrong app: %v", c.link.app)
} }
// test errInvalidFlvTag // test errInvalidFlvTag
_, err = s.Write(tag) var buf [1024]byte
tag := buf[:0]
_, err = c.Write(tag)
if err == nil { if err == nil {
t.Errorf("Write did not return errInvalidFlvTag") t.Errorf("Write did not return errInvalidFlvTag")
} }
// test errUnimplemented // test errUnimplemented
copy(tag, []byte("FLV")) copy(tag, []byte("FLV"))
_, err = s.Write(tag) _, err = c.Write(tag)
if err == nil { if err == nil {
t.Errorf("Write did not return errUnimplemented") t.Errorf("Write did not return errUnimplemented")
} }
// test errInvalidBody // test errInvalidBody
tag = buf[:11] tag = buf[:11]
_, err = s.Write(tag) _, err = c.Write(tag)
if err == nil { if err == nil {
t.Errorf("Write did not return errInvalidBody") t.Errorf("Write did not return errInvalidBody")
} }
err = s.Close() err = c.Close()
if err != nil { if err != nil {
t.Errorf("Close failed with error: %v", err) t.Errorf("Close failed with error: %v", err)
return return
@ -197,10 +176,9 @@ func TestFromFrame(t *testing.T) {
if testKey == "" { if testKey == "" {
t.Skip("Skipping TestFromFrame since no RTMP_TEST_KEY") t.Skip("Skipping TestFromFrame since no RTMP_TEST_KEY")
} }
s := NewSession(testBaseURL+testKey, testTimeout, testLog) c, err := Dial(testBaseURL+testKey, testTimeout, testLog)
err := s.Open()
if err != nil { if err != nil {
t.Errorf("Session.Open failed with error: %v", err) t.Errorf("Dial failed with error: %v", err)
} }
testFrame := os.Getenv("RTMP_TEST_FRAME") testFrame := os.Getenv("RTMP_TEST_FRAME")
@ -216,7 +194,7 @@ func TestFromFrame(t *testing.T) {
} }
const frameRate = 25 const frameRate = 25
rs := &rtmpSender{s: s} rs := &rtmpSender{conn: c}
flvEncoder, err := flv.NewEncoder(rs, true, true, frameRate) flvEncoder, err := flv.NewEncoder(rs, true, true, frameRate)
if err != nil { if err != nil {
t.Errorf("Failed to create flv encoder with error: %v", err) t.Errorf("Failed to create flv encoder with error: %v", err)
@ -226,18 +204,18 @@ func TestFromFrame(t *testing.T) {
t.Errorf("Lexing failed with error: %v", err) t.Errorf("Lexing failed with error: %v", err)
} }
err = s.Close() err = c.Close()
if err != nil { if err != nil {
t.Errorf("Session.Close failed with error: %v", err) t.Errorf("Session.Close failed with error: %v", err)
} }
} }
type rtmpSender struct { type rtmpSender struct {
s *Session conn *Conn
} }
func (rs *rtmpSender) Write(p []byte) (int, error) { func (rs *rtmpSender) Write(p []byte) (int, error) {
n, err := rs.s.Write(p) n, err := rs.conn.Write(p)
if err != errInvalidFlvTag && err != nil { if err != errInvalidFlvTag && err != nil {
return 0, err return 0, err
} }
@ -255,12 +233,10 @@ func TestFromFile(t *testing.T) {
if testKey == "" { if testKey == "" {
t.Skip("Skipping TestFromFile since no RTMP_TEST_KEY") t.Skip("Skipping TestFromFile since no RTMP_TEST_KEY")
} }
s := NewSession(testBaseURL+testKey, testTimeout, testLog) c, err := Dial(testBaseURL+testKey, testTimeout, testLog)
err := s.Open()
if err != nil { if err != nil {
t.Errorf("Session.Open failed with error: %v", err) t.Errorf("Dial failed with error: %v", err)
} }
f, err := os.Open(testFile) f, err := os.Open(testFile)
if err != nil { if err != nil {
t.Errorf("Open failed with error: %v", err) t.Errorf("Open failed with error: %v", err)
@ -268,7 +244,7 @@ func TestFromFile(t *testing.T) {
defer f.Close() defer f.Close()
// Pass RTMP session, true for audio, true for video, and 25 FPS // Pass RTMP session, true for audio, true for video, and 25 FPS
flvEncoder, err := flv.NewEncoder(s, true, true, 25) flvEncoder, err := flv.NewEncoder(c, true, true, 25)
if err != nil { if err != nil {
t.Fatalf("failed to create encoder: %v", err) t.Fatalf("failed to create encoder: %v", err)
} }
@ -277,7 +253,7 @@ func TestFromFile(t *testing.T) {
t.Errorf("Lexing and encoding failed with error: %v", err) t.Errorf("Lexing and encoding failed with error: %v", err)
} }
err = s.Close() err = c.Close()
if err != nil { if err != nil {
t.Errorf("Session.Close failed with error: %v", err) t.Errorf("Session.Close failed with error: %v", err)
} }