github.com/sunvim/utils@v0.1.0/netpoll/net_unix_test.go (about) 1 // Copyright (c) 2020 Meng Huang (mhboy@outlook.com) 2 // This package is licensed under a MIT license that can be found in the LICENSE file. 3 4 package netpoll 5 6 import ( 7 "io" 8 "net" 9 "os" 10 "strings" 11 "sync" 12 "testing" 13 "time" 14 15 "github.com/oxtoacart/bpool" 16 ) 17 18 func TestServerListenAndServe(t *testing.T) { 19 network := "tcp" 20 addr := ":8881" 21 server := &Server{ 22 Network: network, 23 Address: addr, 24 NoAsync: false, 25 } 26 go server.ListenAndServe() 27 time.Sleep(time.Millisecond * 10) 28 server.Close() 29 err := server.ListenAndServe() 30 if err != ErrServerClosed { 31 t.Error(err) 32 } 33 } 34 35 func TestServerServe(t *testing.T) { 36 network := "tcp" 37 addr := ":8882" 38 l, _ := net.Listen(network, addr) 39 server := &Server{ 40 NoAsync: false, 41 } 42 err := server.Serve(nil) 43 if err != ErrListener { 44 t.Error(err) 45 } 46 err = server.Serve(l) 47 if err != ErrHandler { 48 t.Error(err) 49 } 50 server.Handler = nil 51 server.Close() 52 err = server.Serve(l) 53 if err != ErrServerClosed { 54 t.Error(err) 55 } 56 } 57 58 func TestServerPoll(t *testing.T) { 59 var handler = &DataHandler{ 60 Pool: bpool.NewBytePool(1024, 12*1024), 61 HandlerFunc: func(req []byte) (res []byte) { 62 res = req 63 return 64 }, 65 } 66 server := &Server{ 67 Handler: handler, 68 NoAsync: false, 69 } 70 network := "tcp" 71 addr := ":9999" 72 l, _ := net.Listen(network, addr) 73 wg := sync.WaitGroup{} 74 wg.Add(1) 75 go func() { 76 defer wg.Done() 77 server.Serve(l) 78 }() 79 time.Sleep(time.Millisecond * 10) 80 server.accept() 81 time.Sleep(time.Millisecond * 10) 82 server.Close() 83 time.Sleep(time.Millisecond * 10) 84 server.accept() 85 time.Sleep(time.Millisecond * 10) 86 wg.Wait() 87 } 88 89 func TestServerClose(t *testing.T) { 90 var handler = &DataHandler{ 91 Pool: bpool.NewBytePool(1024, 12*1024), 92 HandlerFunc: func(req []byte) (res []byte) { 93 res = req 94 return 95 }, 96 } 97 server := &Server{ 98 Handler: handler, 99 NoAsync: false, 100 } 101 network := "tcp" 102 addr := ":9999" 103 l, _ := net.Listen(network, addr) 104 wg := sync.WaitGroup{} 105 wg.Add(1) 106 go func() { 107 defer wg.Done() 108 server.Serve(l) 109 }() 110 time.Sleep(time.Millisecond * 10) 111 server.Close() 112 server.Close() 113 wg.Wait() 114 } 115 116 func TestServerNumCPU(t *testing.T) { 117 var handler = &DataHandler{ 118 Pool: bpool.NewBytePool(1024, 12*1024), 119 HandlerFunc: func(req []byte) (res []byte) { 120 res = req 121 return 122 }, 123 } 124 temp := numCPU 125 numCPU = 17 126 server := &Server{ 127 Handler: handler, 128 NoAsync: false, 129 } 130 network := "tcp" 131 addr := ":9999" 132 l, _ := net.Listen(network, addr) 133 wg := sync.WaitGroup{} 134 wg.Add(1) 135 go func() { 136 defer wg.Done() 137 server.Serve(l) 138 }() 139 time.Sleep(time.Millisecond * 10) 140 server.Close() 141 wg.Wait() 142 numCPU = temp 143 } 144 145 func TestServerTCPListener(t *testing.T) { 146 var handler = &DataHandler{ 147 Pool: bpool.NewBytePool(1024, 12*1024), 148 HandlerFunc: func(req []byte) (res []byte) { 149 res = req 150 return 151 }, 152 } 153 server := &Server{ 154 Handler: handler, 155 NoAsync: false, 156 } 157 network := "tcp" 158 addr := ":9999" 159 l, _ := net.Listen(network, addr) 160 time.Sleep(time.Millisecond * 10) 161 l.Close() 162 server.Serve(l) 163 server.Close() 164 } 165 166 func TestServerUNIXListener(t *testing.T) { 167 var handler = &DataHandler{ 168 Pool: bpool.NewBytePool(1024, 12*1024), 169 HandlerFunc: func(req []byte) (res []byte) { 170 res = req 171 return 172 }, 173 } 174 server := &Server{ 175 Handler: handler, 176 NoAsync: false, 177 } 178 network := "unix" 179 addr := ":9999" 180 os.Remove(addr) 181 defer os.Remove(addr) 182 l, _ := net.Listen(network, addr) 183 l.Close() 184 server.Serve(l) 185 } 186 187 func TestTCPServer(t *testing.T) { 188 var handler = &DataHandler{ 189 Pool: bpool.NewBytePool(1024, 12*1024), 190 HandlerFunc: func(req []byte) (res []byte) { 191 res = req 192 return 193 }, 194 } 195 handler.SetUpgrade(func(c net.Conn) (net.Conn, error) { 196 var u = &conn{} 197 *u = *(c.(*conn)) 198 return u, nil 199 }) 200 server := &Server{ 201 Handler: handler, 202 NoAsync: false, 203 } 204 network := "tcp" 205 addr := ":9999" 206 l, _ := net.Listen(network, addr) 207 wg := sync.WaitGroup{} 208 wg.Add(1) 209 go func() { 210 defer wg.Done() 211 if err := server.Serve(l); err == nil { 212 t.Error() 213 } 214 }() 215 conn, _ := net.Dial(network, addr) 216 msg := "Hello World" 217 msg = strings.Repeat(msg, 50) 218 if n, err := conn.Write([]byte(msg)); err != nil { 219 t.Error(err) 220 } else if n != len(msg) { 221 t.Error(n) 222 } 223 buf := make([]byte, len(msg)) 224 if n, err := conn.Read(buf); err != nil { 225 t.Error(err) 226 } else if n != len(msg) { 227 t.Error(n) 228 } else if string(buf) != msg { 229 t.Error(string(buf)) 230 } 231 conn.Close() 232 server.Close() 233 wg.Wait() 234 } 235 236 func TestUNIXServer(t *testing.T) { 237 var handler = &DataHandler{ 238 Pool: bpool.NewBytePool(1024, 12*1024), 239 HandlerFunc: func(req []byte) (res []byte) { 240 res = req 241 return 242 }, 243 } 244 server := &Server{ 245 Handler: handler, 246 NoAsync: false, 247 } 248 network := "unix" 249 addr := ":9999" 250 os.Remove(addr) 251 defer os.Remove(addr) 252 l, _ := net.Listen(network, addr) 253 wg := sync.WaitGroup{} 254 wg.Add(1) 255 go func() { 256 defer wg.Done() 257 if err := server.Serve(l); err == nil { 258 t.Error() 259 } 260 }() 261 conn, _ := net.Dial(network, addr) 262 msg := "Hello World" 263 msg = strings.Repeat(msg, 50) 264 if n, err := conn.Write([]byte(msg)); err != nil { 265 t.Error(err) 266 } else if n != len(msg) { 267 t.Error(n) 268 } 269 buf := make([]byte, len(msg)) 270 if n, err := conn.Read(buf); err != nil { 271 t.Error(err) 272 } else if n != len(msg) { 273 t.Error(n) 274 } else if string(buf) != msg { 275 t.Error(string(buf)) 276 } 277 conn.Close() 278 server.Close() 279 wg.Wait() 280 } 281 282 func TestOtherServer(t *testing.T) { 283 type testListener struct { 284 net.Listener 285 } 286 var handler = &DataHandler{ 287 Pool: bpool.NewBytePool(1024, 12*1024), 288 HandlerFunc: func(req []byte) (res []byte) { 289 res = req 290 return 291 }, 292 } 293 server := &Server{ 294 Handler: handler, 295 NoAsync: false, 296 } 297 network := "tcp" 298 addr := ":9999" 299 l, _ := net.Listen(network, addr) 300 wg := sync.WaitGroup{} 301 wg.Add(1) 302 go func() { 303 defer wg.Done() 304 if err := server.Serve(&testListener{l}); err == nil { 305 t.Error() 306 } 307 }() 308 conn, _ := net.Dial(network, addr) 309 msg := "Hello World" 310 msg = strings.Repeat(msg, 50) 311 if n, err := conn.Write([]byte(msg)); err != nil { 312 t.Error(err) 313 } else if n != len(msg) { 314 t.Error(n) 315 } 316 buf := make([]byte, len(msg)) 317 if n, err := conn.Read(buf); err != nil { 318 t.Error(err) 319 } else if n != len(msg) { 320 t.Error(n) 321 } else if string(buf) != msg { 322 t.Error(string(buf)) 323 } 324 conn.Close() 325 server.Close() 326 wg.Wait() 327 } 328 329 func TestShared(t *testing.T) { 330 var handler = &DataHandler{ 331 Pool: bpool.NewBytePool(1024, 12*1024), 332 HandlerFunc: func(req []byte) (res []byte) { 333 res = req 334 return 335 }, 336 } 337 server := &Server{ 338 Handler: handler, 339 NoAsync: false, 340 } 341 network := "tcp" 342 addr := ":9999" 343 l, _ := net.Listen(network, addr) 344 wg := sync.WaitGroup{} 345 wg.Add(1) 346 go func() { 347 defer wg.Done() 348 if err := server.Serve(l); err == nil { 349 t.Error() 350 } 351 }() 352 conn, _ := net.Dial(network, addr) 353 msg := "Hello World" 354 msg = strings.Repeat(msg, 50) 355 if n, err := conn.Write([]byte(msg)); err != nil { 356 t.Error(err) 357 } else if n != len(msg) { 358 t.Error(n) 359 } 360 buf := make([]byte, len(msg)) 361 if n, err := conn.Read(buf); err != nil { 362 t.Error(err) 363 } else if n != len(msg) { 364 t.Error(n) 365 } else if string(buf) != msg { 366 t.Error(string(buf)) 367 } 368 conn.Close() 369 server.Close() 370 wg.Wait() 371 } 372 373 func TestNoCopy(t *testing.T) { 374 var handler = &DataHandler{ 375 Pool: bpool.NewBytePool(1024, 12*1024), 376 HandlerFunc: func(req []byte) (res []byte) { 377 res = req 378 return 379 }, 380 } 381 server := &Server{ 382 Handler: handler, 383 NoAsync: false, 384 } 385 network := "tcp" 386 addr := ":9999" 387 l, _ := net.Listen(network, addr) 388 wg := sync.WaitGroup{} 389 wg.Add(1) 390 go func() { 391 defer wg.Done() 392 if err := server.Serve(l); err == nil { 393 t.Error() 394 } 395 }() 396 conn, _ := net.Dial(network, addr) 397 msg := "Hello World" 398 msg = strings.Repeat(msg, 50) 399 if n, err := conn.Write([]byte(msg)); err != nil { 400 t.Error(err) 401 } else if n != len(msg) { 402 t.Error(n) 403 } 404 buf := make([]byte, len(msg)) 405 if n, err := conn.Read(buf); err != nil { 406 t.Error(err) 407 } else if n != len(msg) { 408 t.Error(n) 409 } else if string(buf) != msg { 410 t.Error(string(buf)) 411 } 412 conn.Close() 413 server.Close() 414 wg.Wait() 415 } 416 417 func TestServerUpgrade(t *testing.T) { 418 var handler = &ConnHandler{} 419 server := &Server{ 420 Handler: handler, 421 NoAsync: false, 422 } 423 network := "tcp" 424 addr := ":9999" 425 l, _ := net.Listen(network, addr) 426 wg := sync.WaitGroup{} 427 wg.Add(1) 428 go func() { 429 defer wg.Done() 430 if err := server.Serve(l); err == nil { 431 t.Error() 432 } 433 }() 434 conn, _ := net.Dial(network, addr) 435 time.Sleep(time.Millisecond * 10) 436 conn.Close() 437 server.Close() 438 wg.Wait() 439 } 440 441 func TestWorker(t *testing.T) { 442 var handler = &ConnHandler{} 443 server := &Server{ 444 Handler: handler, 445 NoAsync: false, 446 } 447 network := "tcp" 448 addr := ":9999" 449 l, _ := net.Listen(network, addr) 450 wg := sync.WaitGroup{} 451 wg.Add(1) 452 go func() { 453 defer wg.Done() 454 if err := server.Serve(l); err == nil { 455 t.Error() 456 } 457 }() 458 time.Sleep(time.Millisecond * 10) 459 server.workers[0].serve(Event{Fd: 0}) 460 server.workers[0].register(&conn{}) 461 server.workers[0].Close() 462 server.Close() 463 wg.Wait() 464 } 465 466 func TestNoAsync(t *testing.T) { 467 var handler = &DataHandler{ 468 Pool: bpool.NewBytePool(1024, 12*1024), 469 HandlerFunc: func(req []byte) (res []byte) { 470 res = req 471 return 472 }, 473 } 474 server := &Server{ 475 Handler: handler, 476 NoAsync: true, 477 } 478 network := "tcp" 479 addr := ":9999" 480 l, _ := net.Listen(network, addr) 481 wg := sync.WaitGroup{} 482 wg.Add(1) 483 go func() { 484 defer wg.Done() 485 if err := server.Serve(l); err == nil { 486 t.Error() 487 } 488 }() 489 conn, _ := net.Dial(network, addr) 490 msg := "Hello World" 491 msg = strings.Repeat(msg, 50) 492 if n, err := conn.Write([]byte(msg)); err != nil { 493 t.Error(err) 494 } else if n != len(msg) { 495 t.Error(n) 496 } 497 buf := make([]byte, len(msg)) 498 if n, err := conn.Read(buf); err != nil { 499 t.Error(err) 500 } else if n != len(msg) { 501 t.Error(n) 502 } else if string(buf) != msg { 503 t.Error(string(buf)) 504 } 505 time.Sleep(time.Millisecond * 500) 506 conn.Close() 507 server.Close() 508 wg.Wait() 509 } 510 511 func TestSharedWorkers(t *testing.T) { 512 var handler = &DataHandler{ 513 Pool: bpool.NewBytePool(1024, 12*1024), 514 HandlerFunc: func(req []byte) (res []byte) { 515 res = req 516 return 517 }, 518 } 519 server := &Server{ 520 Handler: handler, 521 NoAsync: true, 522 UnsharedWorkers: 16, 523 SharedWorkers: numCPU, 524 } 525 network := "tcp" 526 addr := ":9999" 527 l, _ := net.Listen(network, addr) 528 wg := sync.WaitGroup{} 529 wg.Add(1) 530 go func() { 531 defer wg.Done() 532 if err := server.Serve(l); err == nil { 533 t.Error() 534 } 535 }() 536 conn, _ := net.Dial(network, addr) 537 msg := "Hello World" 538 msg = strings.Repeat(msg, 50) 539 if n, err := conn.Write([]byte(msg)); err != nil { 540 t.Error(err) 541 } else if n != len(msg) { 542 t.Error(n) 543 } 544 buf := make([]byte, len(msg)) 545 if n, err := conn.Read(buf); err != nil { 546 t.Error(err) 547 } else if n != len(msg) { 548 t.Error(n) 549 } else if string(buf) != msg { 550 t.Error(string(buf)) 551 } 552 time.Sleep(time.Millisecond * 500) 553 conn.Close() 554 server.Close() 555 wg.Wait() 556 } 557 558 func TestSharedWorkersPanic(t *testing.T) { 559 var handler = &DataHandler{ 560 Pool: bpool.NewBytePool(1024, 12*1024), 561 HandlerFunc: func(req []byte) (res []byte) { 562 res = req 563 return 564 }, 565 } 566 server := &Server{ 567 Handler: handler, 568 NoAsync: true, 569 UnsharedWorkers: 16, 570 SharedWorkers: -1, 571 } 572 defer func() { 573 if p := recover(); p == nil { 574 t.Error() 575 } 576 }() 577 if err := server.Serve(nil); err != nil { 578 t.Error(err) 579 } 580 581 } 582 583 func TestReschedule(t *testing.T) { 584 var handler = &DataHandler{ 585 Pool: bpool.NewBytePool(1024, 12*1024), 586 HandlerFunc: func(req []byte) (res []byte) { 587 res = req 588 return 589 }, 590 } 591 server := &Server{ 592 Handler: handler, 593 NoAsync: false, 594 } 595 network := "tcp" 596 addr := ":9999" 597 l, _ := net.Listen(network, addr) 598 wg := sync.WaitGroup{} 599 wg.Add(1) 600 go func() { 601 defer wg.Done() 602 server.Serve(l) 603 }() 604 connWG := sync.WaitGroup{} 605 for i := 0; i < 20; i++ { 606 connWG.Add(1) 607 go func() { 608 defer connWG.Done() 609 conn, _ := net.Dial(network, addr) 610 msg := "Hello World" 611 msg = strings.Repeat(msg, 50) 612 when := time.Now().Add(time.Second) 613 for time.Now().Before(when) { 614 if n, err := conn.Write([]byte(msg)); err != nil { 615 t.Error(err) 616 } else if n != len(msg) { 617 t.Error(n) 618 } 619 buf := make([]byte, len(msg)) 620 if n, err := conn.Read(buf); err != nil { 621 t.Error(err) 622 } else if n != len(msg) { 623 t.Error(n) 624 } else if string(buf) != msg { 625 t.Error(string(buf)) 626 } 627 } 628 conn.Close() 629 }() 630 } 631 632 connWG.Wait() 633 for i := 0; i < 512; i++ { 634 go server.reschedule() 635 } 636 time.Sleep(time.Millisecond * 1010) 637 server.Close() 638 time.Sleep(time.Millisecond * 10) 639 server.rescheduled = false 640 server.reschedule() 641 wg.Wait() 642 } 643 644 func TestRescheduleDone(t *testing.T) { 645 var handler = &DataHandler{ 646 Pool: bpool.NewBytePool(1024, 12*1024), 647 HandlerFunc: func(req []byte) (res []byte) { 648 res = req 649 return 650 }, 651 } 652 server := &Server{ 653 Handler: handler, 654 NoAsync: false, 655 } 656 network := "tcp" 657 addr := ":9999" 658 l, _ := net.Listen(network, addr) 659 wg := sync.WaitGroup{} 660 wg.Add(1) 661 go func() { 662 defer wg.Done() 663 server.Serve(l) 664 }() 665 connWG := sync.WaitGroup{} 666 for i := 0; i < 32; i++ { 667 connWG.Add(1) 668 go func() { 669 defer connWG.Done() 670 conn, _ := net.Dial(network, addr) 671 msg := "Hello World" 672 msg = strings.Repeat(msg, 50) 673 when := time.Now().Add(time.Second) 674 if i > 20 { 675 for j := 0; j < 4; j++ { 676 for time.Now().Before(when) { 677 if n, err := conn.Write([]byte(msg)); err != nil { 678 t.Error(err) 679 } else if n != len(msg) { 680 t.Error(n) 681 } 682 buf := make([]byte, len(msg)) 683 if n, err := conn.Read(buf); err != nil { 684 t.Error(err) 685 } else if n != len(msg) { 686 t.Error(n) 687 } else if string(buf) != msg { 688 t.Error(string(buf)) 689 } 690 time.Sleep(time.Millisecond) 691 } 692 } 693 } 694 for time.Now().Before(when) { 695 if n, err := conn.Write([]byte(msg)); err != nil { 696 t.Error(err) 697 } else if n != len(msg) { 698 t.Error(n) 699 } 700 buf := make([]byte, len(msg)) 701 if n, err := conn.Read(buf); err != nil { 702 t.Error(err) 703 } else if n != len(msg) { 704 t.Error(n) 705 } else if string(buf) != msg { 706 t.Error(string(buf)) 707 } 708 } 709 conn.Close() 710 }() 711 } 712 713 connWG.Wait() 714 server.Close() 715 server.rescheduled = false 716 server.wakeReschedule() 717 go server.wakeReschedule() 718 wg.Wait() 719 } 720 721 func TestRawConn(t *testing.T) { 722 conn := &conn{fd: 1} 723 rawConn, _ := conn.SyscallConn() 724 rawConn.Control(func(fd uintptr) { 725 if fd != 1 { 726 t.Error(fd) 727 } 728 }) 729 rawConn.Read(func(fd uintptr) (done bool) { 730 if fd != 1 { 731 t.Error(fd) 732 } 733 return true 734 }) 735 rawConn.Write(func(fd uintptr) (done bool) { 736 if fd != 1 { 737 t.Error(fd) 738 } 739 return true 740 }) 741 conn.closed = 1 742 rawConn.Control(func(fd uintptr) { 743 if fd != 1 { 744 t.Error(fd) 745 } 746 }) 747 rawConn.Read(func(fd uintptr) (done bool) { 748 if fd != 1 { 749 t.Error(fd) 750 } 751 return true 752 }) 753 rawConn.Write(func(fd uintptr) (done bool) { 754 if fd != 1 { 755 t.Error(fd) 756 } 757 return true 758 }) 759 } 760 761 func TestSplice(t *testing.T) { 762 var handler = &ConnHandler{} 763 handler.SetUpgrade(func(conn net.Conn) (Context, error) { 764 return conn, nil 765 }) 766 handler.SetServe(func(context Context) error { 767 conn := context.(net.Conn) 768 _, err := io.Copy(conn, conn) 769 return err 770 }) 771 server := &Server{ 772 Handler: handler, 773 NoAsync: false, 774 } 775 network := "tcp" 776 addr := ":9999" 777 l, _ := net.Listen(network, addr) 778 wg := sync.WaitGroup{} 779 wg.Add(1) 780 go func() { 781 defer wg.Done() 782 if err := server.Serve(l); err == nil { 783 t.Error() 784 } 785 }() 786 conn, _ := net.Dial(network, addr) 787 msg := "Hello World" 788 msg = strings.Repeat(msg, 50) 789 if n, err := conn.Write([]byte(msg)); err != nil { 790 t.Error(err) 791 } else if n != len(msg) { 792 t.Error(n) 793 } 794 buf := make([]byte, len(msg)) 795 if n, err := conn.Read(buf); err != nil { 796 t.Error(err) 797 } else if n != len(msg) { 798 t.Error(n) 799 } else if string(buf) != msg { 800 t.Error(string(buf)) 801 } 802 conn.Close() 803 server.Close() 804 wg.Wait() 805 } 806 807 func TestSendFile(t *testing.T) { 808 srcName := "srcfile" 809 srcFile, err := os.Create(srcName) 810 if err != nil { 811 panic(err) 812 } 813 defer os.Remove(srcName) 814 defer srcFile.Close() 815 contents := "Hello world" 816 offset := 10 817 if offset > 0 { 818 srcFile.Write(make([]byte, offset)) 819 } 820 var handler = &ConnHandler{} 821 handler.SetUpgrade(func(conn net.Conn) (Context, error) { 822 return conn, nil 823 }) 824 handler.SetServe(func(context Context) error { 825 conn := context.(net.Conn) 826 buf := make([]byte, len(contents)) 827 if n, err := conn.Read(buf); n <= 0 { 828 return err 829 } else if string(buf) != contents { 830 t.Error(string(buf)) 831 } 832 srcFile.Write([]byte(contents)) 833 srcFile.Sync() 834 io.Copy(conn, srcFile) 835 srcFile.Seek(int64(offset), os.SEEK_SET) 836 _, err := io.Copy(conn, srcFile) 837 return err 838 }) 839 server := &Server{ 840 Handler: handler, 841 NoAsync: false, 842 } 843 network := "tcp" 844 addr := ":9999" 845 l, _ := net.Listen(network, addr) 846 wg := sync.WaitGroup{} 847 wg.Add(1) 848 go func() { 849 defer wg.Done() 850 if err := server.Serve(l); err == nil { 851 t.Error() 852 } 853 }() 854 conn, _ := net.Dial(network, addr) 855 if n, err := conn.Write([]byte(contents)); err != nil { 856 t.Error(err) 857 } else if n != len(contents) { 858 t.Error(n) 859 } 860 buf := make([]byte, len(contents)) 861 if n, err := conn.Read(buf); err != nil { 862 t.Error(err) 863 } else if n != len(contents) { 864 t.Error(n) 865 } else if string(buf) != contents { 866 t.Error(string(buf)) 867 } 868 conn.Close() 869 server.Close() 870 wg.Wait() 871 } 872 873 func TestReadFromLimitedReader(t *testing.T) { 874 var handler = &ConnHandler{} 875 handler.SetUpgrade(func(conn net.Conn) (Context, error) { 876 return conn, nil 877 }) 878 handler.SetServe(func(context Context) error { 879 conn := context.(net.Conn) 880 io.CopyN(conn, conn, 0) 881 _, err := io.CopyN(conn, conn, bufferSize) 882 return err 883 }) 884 server := &Server{ 885 Handler: handler, 886 NoAsync: false, 887 } 888 network := "tcp" 889 addr := ":9999" 890 l, _ := net.Listen(network, addr) 891 wg := sync.WaitGroup{} 892 wg.Add(1) 893 go func() { 894 defer wg.Done() 895 if err := server.Serve(l); err == nil { 896 t.Error() 897 } 898 }() 899 conn, _ := net.Dial(network, addr) 900 msg := "Hello World" 901 msg = strings.Repeat(msg, 50) 902 if n, err := conn.Write([]byte(msg)); err != nil { 903 t.Error(err) 904 } else if n != len(msg) { 905 t.Error(n) 906 } 907 buf := make([]byte, len(msg)) 908 if n, err := conn.Read(buf); err != nil { 909 t.Error(err) 910 } else if n != len(msg) { 911 t.Error(n) 912 } else if string(buf) != msg { 913 t.Error(string(buf)) 914 } 915 conn.Close() 916 server.Close() 917 wg.Wait() 918 } 919 920 func TestGenericReadFrom(t *testing.T) { 921 var handler = &ConnHandler{} 922 handler.SetUpgrade(func(conn net.Conn) (Context, error) { 923 return conn, nil 924 }) 925 handler.SetServe(func(context Context) error { 926 conn := context.(net.Conn) 927 buf := make([]byte, bufferSize) 928 r, w := io.Pipe() 929 n, _ := conn.Read(buf) 930 go w.Write(buf[:n]) 931 conn.(io.ReaderFrom).ReadFrom(r) 932 return EAGAIN 933 }) 934 server := &Server{ 935 Handler: handler, 936 NoAsync: false, 937 } 938 network := "tcp" 939 addr := ":9999" 940 l, _ := net.Listen(network, addr) 941 wg := sync.WaitGroup{} 942 wg.Add(1) 943 go func() { 944 defer wg.Done() 945 if err := server.Serve(l); err == nil { 946 t.Error() 947 } 948 }() 949 conn, _ := net.Dial(network, addr) 950 msg := "Hello World" 951 msg = strings.Repeat(msg, 50) 952 if n, err := conn.Write([]byte(msg)); err != nil { 953 t.Error(err) 954 } else if n != len(msg) { 955 t.Error(n) 956 } 957 buf := make([]byte, len(msg)) 958 if n, err := conn.Read(buf); err != nil { 959 t.Error(err) 960 } else if n != len(msg) { 961 t.Error(n) 962 } else if string(buf) != msg { 963 t.Error(string(buf)) 964 } 965 conn.Close() 966 server.Close() 967 wg.Wait() 968 } 969 970 func TestGenericReadFromRemain(t *testing.T) { 971 genericReadFrom(nil, nil, -1) 972 var handler = &ConnHandler{} 973 handler.SetUpgrade(func(conn net.Conn) (Context, error) { 974 return conn, nil 975 }) 976 handler.SetServe(func(context Context) error { 977 conn := context.(net.Conn) 978 _, err := genericReadFrom(conn, conn, bufferSize+1) 979 return err 980 }) 981 server := &Server{ 982 Handler: handler, 983 NoAsync: false, 984 } 985 network := "tcp" 986 addr := ":9999" 987 l, _ := net.Listen(network, addr) 988 wg := sync.WaitGroup{} 989 wg.Add(1) 990 go func() { 991 defer wg.Done() 992 if err := server.Serve(l); err == nil { 993 t.Error() 994 } 995 }() 996 conn, _ := net.Dial(network, addr) 997 msg := "Hello World" 998 msg = strings.Repeat(msg, 50) 999 if n, err := conn.Write([]byte(msg)); err != nil { 1000 t.Error(err) 1001 } else if n != len(msg) { 1002 t.Error(n) 1003 } 1004 buf := make([]byte, len(msg)) 1005 if n, err := conn.Read(buf); err != nil { 1006 t.Error(err) 1007 } else if n != len(msg) { 1008 t.Error(n) 1009 } else if string(buf) != msg { 1010 t.Error(string(buf)) 1011 } 1012 conn.Close() 1013 server.Close() 1014 wg.Wait() 1015 } 1016 1017 func TestTopK(t *testing.T) { 1018 { 1019 l := list{&conn{score: 10}, &conn{score: 7}, &conn{score: 2}, &conn{score: 5}, &conn{score: 1}} 1020 k := 3 1021 topK(l, k) 1022 for j := k; j < l.Len(); j++ { 1023 for i := 0; i < k; i++ { 1024 if l.Less(i, j) { 1025 t.Error("top error") 1026 } 1027 } 1028 } 1029 } 1030 { 1031 l := list{&conn{score: 10}, &conn{score: 7}, &conn{score: 2}, &conn{score: 5}, &conn{score: 1}} 1032 k := 9 1033 topK(l, k) 1034 n := l.Len() 1035 for i := 1; i < n; i++ { 1036 if l.Less(i, 0) { 1037 t.Error("heap error") 1038 } 1039 } 1040 } 1041 } 1042 1043 func TestMinHeap(t *testing.T) { 1044 { 1045 l := workers{&worker{count: 10}, &worker{count: 7}, &worker{count: 2}, &worker{count: 5}, &worker{count: 1}} 1046 minHeap(l) 1047 for j := 1; j < l.Len(); j++ { 1048 if !l.Less(0, j) { 1049 t.Error("minHeap error") 1050 } 1051 } 1052 } 1053 }