Ejemplo n.º 1
0
func TestClosePort(t *testing.T) {
	l := NewListener()
	l.AddPort(56561)
	gcnt := runtime.Goroutines()
	l.ClosePort(56561)
	// ClosePort is not synchronized, so give it some time (on mac, dialog pops up)
	for i := 0; i < 100 && 0 != len(l.ports); i++ {
		time.Sleep(1e6)
	}
	if runtime.Gosched(); 0 != len(l.ports) {
		t.Errorf("After ClosePort(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.Goroutines() {
		t.Errorf("Expected fewer than %d goroutines after ClosePort(), %d running", gcnt,
			runtime.Goroutines())
	}
	l.Close()
	if 0 != len(l.ports) {
		t.Errorf("After Close(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.Goroutines() {
		t.Errorf("Expected fewer than %d goroutines after Close(), %d running", gcnt,
			runtime.Goroutines())
	}
}
Ejemplo n.º 2
0
func Test2(t *testing.T) {
	ch1, ch2 := make(chan bool), make(chan bool)
	go func() {
		for {
			bytes, _ := ioutil.ReadFile("bigfile")
			println("2", len(bytes))
		}
		ch2 <- true
	}()

	i := 0
	go func() {
		for {
			i++
			// time.Sleep(1000000)
			if i == 1000000000 {
				println("tick")
				i = 0
			}
		}
		ch1 <- true
	}()

	println("i =", i)
	println("grc =", runtime.Goroutines())

	<-ch1
	<-ch2
}
Ejemplo n.º 3
0
func StatsServer(w http.ResponseWriter, req *http.Request) {
	stats := runtime.MemStats

	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	fmt.Fprintf(w, "Total allocated: %d bytes, In-use: %d bytes\n", stats.TotalAlloc, stats.Alloc)
	fmt.Fprintf(w, "Heap in-use: %d bytes, Number of heap objects: %d\n", stats.HeapAlloc, stats.HeapObjects)
	fmt.Fprintf(w, "There are %d Goroutines in the system\n", runtime.Goroutines())
}
Ejemplo n.º 4
0
func TestGoroutines(t *testing.T) {
	gs := runtime.Goroutines()

	func() {
		l := mustListen()
		defer l.Close()
		u := mustListenPacket(l.Addr().String())
		defer u.Close()

		go Main("a", "", u, l, nil)

		cl, err := client.Dial(l.Addr().String())
		assert.Equal(t, nil, err)
		cl.Noop()
	}()

	assert.T(t, gs+leaked >= runtime.Goroutines(), gs+leaked)
}
Ejemplo n.º 5
0
func (s *Stats) SummaryLine() string {
	s.lk.Lock()
	defer s.lk.Unlock()
	return fmt.Sprintf("Running %d mins, %d accept, %d expire, %d req, %d resp; MaxReqRespTime: %dms; %d goroutine",
		(time.Nanoseconds()-s.TimeStarted)/(60*1e9),
		s.AcceptConnCount, s.ExpireConnCount, s.RequestCount, s.ResponseCount,
		s.MaxReqRespTime/1e6,
		runtime.Goroutines())
}
Ejemplo n.º 6
0
func init() {
	start := time.Seconds()
	Publish("runtime", map[string]interface{}{
		"cgocalls":   Func(func() interface{} { return runtime.Cgocalls() }),
		"goroutines": Func(func() interface{} { return runtime.Goroutines() }),
		"version":    runtime.Version(),
		"memstats":   &runtime.MemStats,
	})
	Publish("uptimeSeconds", Func(func() interface{} { return time.Seconds() - start }))
	Publish("cmdline", &os.Args)
}
Ejemplo n.º 7
0
func TestAddPort(t *testing.T) {
	l := NewListener()
	gcnt := runtime.Goroutines()
	l.AddPort(56561)
	if 1 != len(l.ports) {
		t.Errorf("Length of ports array should be 1, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt >= runtime.Goroutines() {
		t.Errorf("Expected more than %d goroutines after AddPort, %d running", gcnt,
			runtime.Goroutines())
	}
	if listener, ok := l.ports[56561]; ok {
		if listener == nil {
			t.Errorf("Port listener should not be nil")
		}
	} else {
		t.Errorf("Listener should have entry for port 56561, got %v", l.ports)
	}
	gcnt = runtime.Goroutines()
	l.Close()
	if 0 != len(l.ports) {
		t.Errorf("After Close(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.Goroutines() {
		t.Errorf("Expected fewer than %d goroutines after Close(), %d running", gcnt,
			runtime.Goroutines())
	}
}
Ejemplo n.º 8
0
func main() {
	netf := flag.String("net", "irc.freenode.net", "Network address")
	port := flag.String("port", "6667", "Network port")
	passf := flag.String("pass", "", "Network Password")
	nickf := flag.String("nick", "go-irc-chans", "Nickname on network")
	userf := flag.String("user", "", "Irc user (defaults to nick)")
	rnf := flag.String("realname", "", "Real Name (defaults to nick)")
	chans := flag.String("chans", "#go-nuts", "Channles to join separated by commas (e.g. #foo,#bar; defaults to #go-nuts)")
	logfile := flag.String("logfile", "", "File used for logging (default: stderr)")
	usage := flag.Bool("h", false, "Display usage and help message")
	flag.Parse()
	if *usage {
		flag.PrintDefaults()
		os.Exit(0)
	}
	if *userf == "" {
		userf = nickf
	}
	if *rnf == "" {
		rnf = nickf
	}
	log.Println(strings.Join([]string{*netf, *port}, ":"), *nickf, *userf, *rnf, *passf, *logfile)
	channels := strings.Split(*chans, ",", -1)
	n := ircchans.NewNetwork(*netf, *port, *nickf, *userf, *rnf, *passf, *logfile)
	//test replies, outgoing messages
	go func() {
		chin := make(chan *ircchans.IrcMessage, 100)
		n.Listen.RegListener("PRIVMSG", "testreply", chin)
		for !closed(chin) {
			msg := <-chin
			nick := n.GetNick()
			if msg.Destination() == nick {
				switch strings.Join(msg.Params[1:], " ") {
				case "memusage":
					targ := strings.Split(msg.Prefix, "!", 2)
					n.Privmsg([]string{targ[0]}, fmt.Sprintf("Currently allocated: %.2fMb, taken from system: %.2fMb", float32(runtime.MemStats.Alloc)/1024/1024, float32(runtime.MemStats.Sys)/1024/1024))
					n.Privmsg([]string{targ[0]}, fmt.Sprintf("Currently allocated (heap): %.2fMb, taken from system (heap): %.2fMb", float32(runtime.MemStats.HeapAlloc)/1024/1024, float32(runtime.MemStats.HeapSys)/1024/1024))
					n.Privmsg([]string{targ[0]}, fmt.Sprintf("Goroutines currently running: %d", runtime.Goroutines()))
					n.Privmsg([]string{targ[0]}, fmt.Sprintf("Next garbage collection will be when heap reaches %.1f Mb.", float32(runtime.MemStats.NextGC)/1024/1024))
				case "reconnect":
					n.Disconnect("Order")
				}
			}
		}
		n.Listen.DelListener("PRIVMSG", "testreply")
	}()
	ticker := time.Tick(1000 * 1000 * 1000 * 15)
	ticker15 := time.Tick(1000 * 1000 * 1000 * 60 * 15)
	for !closed(ticker) {
		select {
		case <-ticker:
			fmt.Println(time.LocalTime())
			if n.Disconnected {
				fmt.Println("Disconnected")
				for err := n.Connect(); err != nil; err = n.Connect() {
					fmt.Printf("Connection failed: %s", err.String())
					time.Sleep(minute / 12)
				}
				if err := n.Join(channels, []string{}); err != nil {
					fmt.Printf("Error joining channels %v\n", channels)
					os.Exit(1)
				}
			}
		case <-ticker15:
			nick, _ := n.Nick("")
			fmt.Println(n.Whois([]string{nick}, ""))
		}
	}
	os.Exit(0)
}