Пример #1
0
func Test_CrashInCatch(T *testing.T) {
	calledFinally := false

	defer func() {
		err := recover()
		if err != "another panic" {
			T.Error("error is not 'another panic'")
		}
		// if finally was not called
		if !calledFinally {
			T.Error("Finally do not called")
		}
	}()
	try.This(func() {
		panic("testing panic")

	}).Finally(func() {
		calledFinally = true

	}).Catch(func(e try.E) {
		if e != "testing panic" {
			T.Error("error is not 'testing panic'")
		}
		panic("another panic")

	})
}
Пример #2
0
func Test_CrashInTry(T *testing.T) {
	calledFinally := false
	calledCatch := false

	try.This(func() {
		panic("testing panic")

	}).Finally(func() {
		calledFinally = true

	}).Catch(func(e try.E) {
		calledCatch = true
		if e != "testing panic" {
			T.Error("error is not 'testing panic'")
		}
	})

	// if catch was not called
	if !calledCatch {
		T.Error("Catch do not called")
	}

	// if finally was not called
	if !calledFinally {
		T.Error("Finally do not called")
	}
}
Пример #3
0
func Test_NormalFlowFinally(T *testing.T) {
	calledTry := false
	calledFinally := false

	try.This(func() {
		calledTry = true

	}).Finally(func() {
		calledFinally = true

	}).Catch(func(_ try.E) {
		T.Error("Catch must not be called")

	})

	// if try was not called
	if !calledTry {
		T.Error("Try do not called")
	}

	// if finally was not called
	if !calledFinally {
		T.Error("Finally do not called")
	}
}
Пример #4
0
func Test_CrashInFinally1(T *testing.T) {
	calledTry := false

	defer func() {
		err := recover()
		if err != "finally panic" {
			T.Error("error is not 'finally panic'")
		}

		// if try was not called
		if !calledTry {
			T.Error("Try do not called")
		}
	}()

	try.This(func() {
		calledTry = true

	}).Finally(func() {
		panic("finally panic")

	}).Catch(func(e try.E) {
		T.Error("Catch must not be called")
	})
}
Пример #5
0
func Try(ctx *neo.Ctx, next neo.Next) {

	try.This(func() {
		next()
	}).Catch(func(e try.E) {
		// Print error
		ctx.Res.Status = 500
		ctx.Res.Text(fmt.Sprint(e))
	})

}
Пример #6
0
func main() {
	try.This(func() {
		a := 10
		b := 0
		c := 0

		c = a / b
		fmt.Printf("result = %.2f\n", c)
	}).Finally(func() {
		fmt.Println("Done")
	}).Catch(func(_ try.E) {
		fmt.Println("exception catched")
		try.Throw() // rethrow current exception
	})
}
Пример #7
0
func Test_NormalFlow(T *testing.T) {
	called := false

	try.This(func() {
		called = true

	}).Catch(func(_ try.E) {
		T.Error("Catch must not be called")
	})

	// if try was not called
	if !called {
		T.Error("Try do not called")
	}
}
Пример #8
0
func Test_CrashInCatch2(T *testing.T) {
	defer func() {
		err := recover()
		if err != "another panic" {
			T.Error("error is not 'another panic'")
		}
	}()
	try.This(func() {
		panic("testing panic")

	}).Catch(func(e try.E) {
		if e != "testing panic" {
			T.Error("error is not 'testing panic'")
		}
		panic("another panic")
	})
}
Пример #9
0
func (s *RtmpNetStream) readLoop() {
	log.Info(s.conn.conn.RemoteAddr(), "->", s.conn.conn.LocalAddr(), "readLoop running")
	defer log.Info(s.conn.conn.RemoteAddr(), "->", s.conn.conn.LocalAddr(), "readLoop stopped")
	conn := s.conn
	var err error
	var msg RtmpMessage
	for {
		if s.isClosed() {
			break
		}
		try.This(func() {
			msg, err = readMessage(conn)
			if err != nil {
				s.notifyError(err)
				return
			}
			//log.Debug("<<<<< ", msg)
			if msg.Header().MessageLength <= 0 {
				return
			}
			if am, ok := msg.(*AudioMessage); ok {
				//log.Info("AUDIO", ">>>>", am.Header().Timestamp, am.Header().MessageLength)
				sp := NewMediaFrame()
				sp.Timestamp = am.RtmpHeader.Timestamp
				if am.RtmpHeader.Timestamp == 0xffffff {
					sp.Timestamp = am.RtmpHeader.ExtendTimestamp
				}
				sp.Type = am.RtmpHeader.MessageType
				sp.StreamId = am.RtmpHeader.StreamId
				sp.Payload.Write(am.Payload)
				one := am.Payload[0]
				sp.AudioFormat = one >> 4
				sp.SamplingRate = (one & 0x0c) >> 2
				sp.SampleLength = (one & 0x02) >> 1
				sp.AudioType = one & 0x01
				err = s.obj.WriteFrame(sp)
				if err != nil {
					log.Warn(err)
				}
			} else if vm, ok := msg.(*VideoMessage); ok {
				//log.Info("VIDEO", ">>>>", vm.Header().Timestamp, vm.Header().MessageLength)
				sp := NewMediaFrame()
				sp.Timestamp = vm.RtmpHeader.Timestamp
				if vm.RtmpHeader.Timestamp == 0xffffff {
					sp.Timestamp = vm.RtmpHeader.ExtendTimestamp
				}
				sp.Type = vm.RtmpHeader.MessageType
				sp.StreamId = vm.RtmpHeader.StreamId
				sp.Payload.Write(vm.Payload)
				one := vm.Payload[0]
				sp.VideoFrameType = one >> 4
				sp.VideoCodecID = one & 0x0f
				err = s.obj.WriteFrame(sp)
				if err != nil {
					log.Warn(err)
				}
			} else if mm, ok := msg.(*MetadataMessage); ok {
				sp := NewMediaFrame()
				sp.Timestamp = mm.RtmpHeader.Timestamp
				if mm.RtmpHeader.Timestamp == 0xffffff {
					sp.Timestamp = mm.RtmpHeader.ExtendTimestamp
				}
				sp.Type = mm.RtmpHeader.MessageType
				sp.StreamId = mm.RtmpHeader.StreamId
				sp.Payload.Write(mm.Payload)
				err = s.obj.WriteFrame(sp)
				if err != nil {
					log.Warn(err)
				}
				//s.videochan <- sp
				log.Debug("IN", " <<<<<<< ", mm)
			} else if csm, ok := msg.(*CreateStreamMessage); ok {
				log.Debug("IN", " <<<<<<< ", csm)
				conn.streamid = conn.nextStreamId(csm.RtmpHeader.ChunkId)
				err := sendCreateStreamResult(conn, csm.TransactionId)
				if err != nil {
					s.notifyError(err)
					return
				}
			} else if m, ok := msg.(*PublishMessage); ok {
				log.Debug("IN", " <<<<<<< ", m)
				if strings.HasSuffix(conn.app, "/") {
					s.path = conn.app + strings.Split(m.PublishName, "?")[0]

				} else {
					s.path = conn.app + "/" + strings.Split(m.PublishName, "?")[0]
				}
				s.streamName = s.path
				if err := s.notifyPublishing(); err != nil {
					err = sendPublishResult(conn, "error", err.Error())
					if err != nil {
						s.notifyError(err)
					}
					return
				}
				err := sendStreamBegin(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				err = sendPublishStart(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				if s.mode == 0 {
					s.mode = MODE_PRODUCER
				} else {
					s.mode = s.mode | MODE_PRODUCER
				}

			} else if m, ok := msg.(*PlayMessage); ok {
				log.Debug("IN", " <<<<<<< ", m)
				if strings.HasSuffix(s.conn.app, "/") {
					s.path = s.conn.app + strings.Split(m.StreamName, "?")[0]
				} else {
					s.path = s.conn.app + "/" + strings.Split(m.StreamName, "?")[0]
				}
				s.streamName = s.path
				conn.writeChunkSize = 512 //RTMP_MAX_CHUNK_SIZE
				err = s.notifyPlaying()
				if err != nil {
					err = sendPlayResult(conn, "error", err.Error())
					if err != nil {
						s.notifyError(err)
					}
					return
				}
				err := sendChunkSize(conn, uint32(conn.writeChunkSize))
				if err != nil {
					s.notifyError(err)
					return
				}
				err = sendStreamRecorded(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				err = sendStreamBegin(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				err = sendPlayReset(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				err = sendPlayStart(conn)
				if err != nil {
					s.notifyError(err)
					return
				}
				if s.mode == 0 {
					s.mode = MODE_CONSUMER
				} else {
					s.mode = s.mode | MODE_CONSUMER
				}
			} else if m, ok := msg.(*CloseStreamMessage); ok {
				log.Debug("IN", " <<<<<<< ", m)
				s.Close()
			} else {
				log.Debug("IN Why?", " <<<<<<< ", msg)
			}
		}).Catch(func(e try.E) {
			log.Error(e)
			s.Close()
		})
	}
}