z_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package ami
  2. import (
  3. "log"
  4. "os"
  5. "testing"
  6. "time"
  7. "git.ali33.ru/fcg-xvii/go-tools/text/config"
  8. _ "git.ali33.ru/fcg-xvii/go-tools/text/config/ini"
  9. )
  10. var (
  11. host, login, password string
  12. )
  13. func init() {
  14. // setup auth vars, z_auth.config file example :
  15. // 127.0.0.1:5038::admin::mypassword
  16. //config.SplitFileToVals("z_auth.config", "::", &host, &login, &password)
  17. f, err := os.Open("z_auth.config")
  18. if err == nil {
  19. conf, err := config.FromReader("ini", f)
  20. if err == nil {
  21. conf.ValueSetup("host", &host)
  22. conf.ValueSetup("login", &login)
  23. conf.ValueSetup("password", &password)
  24. }
  25. f.Close()
  26. }
  27. }
  28. func TestClient(t *testing.T) {
  29. if host == "" {
  30. return
  31. }
  32. log.Println(host, login, password)
  33. var cl *Client
  34. cl = New(host, login, password, nil, func(state State, err error) {
  35. log.Println("STATE_CHANGED", state, err)
  36. switch state {
  37. case StateStopped:
  38. {
  39. time.Sleep(time.Second * 5)
  40. log.Println("Reconnect...")
  41. //go cl.Start()
  42. }
  43. }
  44. })
  45. go cl.Start()
  46. for {
  47. log.Println("Originate...")
  48. rOrig := &OriginateRequest{
  49. Channel: "SIP/user1/89774708408",
  50. Context: "admefine-bot",
  51. Exten: "s",
  52. }
  53. originate, err := cl.Originate(rOrig)
  54. if err != nil {
  55. log.Println("Originate error", err)
  56. continue
  57. }
  58. log.Println("Call start")
  59. for event := range originate.Events() {
  60. log.Println(event)
  61. }
  62. log.Println("Call finished...")
  63. //time.Sleep(time.Second * 5)
  64. cl.Close()
  65. }
  66. }