lkcp9_sess_test.go 12 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  2. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  3. // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
  4. // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. package lkcp9_test
  11. import (
  12. "fmt"
  13. "hash/fnv"
  14. "io"
  15. "log"
  16. "net"
  17. "net/http"
  18. _ "net/http/pprof"
  19. "runtime"
  20. "runtime/debug"
  21. "sync"
  22. "testing"
  23. "time"
  24. u "go.gridfinity.dev/leaktestfe"
  25. "go.gridfinity.dev/lkcp9"
  26. "golang.org/x/crypto/pbkdf2"
  27. )
  28. const (
  29. portEcho = "127.0.0.1:9079"
  30. portSink = "127.0.0.1:19609"
  31. portTinyBufferEcho = "127.0.0.1:29609"
  32. portListerner = "127.0.0.1:9078"
  33. )
  34. var (
  35. key = []byte(
  36. "testkey",
  37. )
  38. pass = pbkdf2.Key(
  39. key,
  40. []byte(portSink),
  41. 4096,
  42. 32,
  43. fnv.New128a,
  44. )
  45. )
  46. func init() {
  47. go func() {
  48. log.Println(
  49. http.ListenAndServe(
  50. "127.0.0.1:8881",
  51. nil,
  52. ))
  53. }()
  54. go echoServer()
  55. go sinkServer()
  56. go tinyBufferEchoServer()
  57. println(
  58. "Beginning tests:\nEncryption: Salsa20n\nFEC: 10/3",
  59. )
  60. }
  61. func dialEcho() (
  62. *lkcp9.UDPSession,
  63. error,
  64. ) {
  65. block, _ := lkcp9.NewSalsa20BlockCrypt(
  66. pass,
  67. )
  68. sess, err := lkcp9.DialWithOptions(
  69. portEcho,
  70. block,
  71. 10,
  72. 3,
  73. )
  74. if err != nil {
  75. panic(
  76. err,
  77. )
  78. }
  79. sess.SetStreamMode(
  80. true,
  81. )
  82. sess.SetStreamMode(
  83. false,
  84. )
  85. sess.SetStreamMode(
  86. true,
  87. )
  88. sess.SetWindowSize(
  89. 1024,
  90. 1024,
  91. )
  92. sess.SetReadBuffer(
  93. 16 * 1024 * 1024,
  94. )
  95. sess.SetWriteBuffer(
  96. 16 * 1024 * 1024,
  97. )
  98. sess.SetStreamMode(
  99. true,
  100. )
  101. sess.SetNoDelay(
  102. 1, 10, 2, 1,
  103. )
  104. sess.SetMtu(
  105. 1400,
  106. )
  107. sess.SetMtu(
  108. 1600,
  109. )
  110. sess.SetMtu(
  111. 1400,
  112. )
  113. sess.SetACKNoDelay(
  114. true,
  115. )
  116. sess.SetACKNoDelay(
  117. false,
  118. )
  119. sess.SetDeadline(time.Now().Add(
  120. time.Minute,
  121. ))
  122. runtime.GC()
  123. debug.FreeOSMemory()
  124. return sess, err
  125. }
  126. func dialSink() (
  127. *lkcp9.UDPSession,
  128. error,
  129. ) {
  130. sess, err := lkcp9.DialWithOptions(
  131. portSink,
  132. nil,
  133. 0,
  134. 0,
  135. )
  136. if err != nil {
  137. panic(
  138. err,
  139. )
  140. }
  141. sess.SetStreamMode(
  142. true,
  143. )
  144. sess.SetWindowSize(
  145. 1024, 1024,
  146. )
  147. sess.SetReadBuffer(
  148. 16 * 1024 * 1024,
  149. )
  150. sess.SetWriteBuffer(
  151. 16 * 1024 * 1024)
  152. sess.SetStreamMode(
  153. true,
  154. )
  155. sess.SetNoDelay(
  156. 1, 10, 2, 1,
  157. )
  158. sess.SetMtu(
  159. 1400,
  160. )
  161. sess.SetACKNoDelay(
  162. false,
  163. )
  164. sess.SetDeadline(
  165. time.Now().Add(
  166. time.Minute,
  167. ),
  168. )
  169. return sess, err
  170. }
  171. func dialTinyBufferEcho() (
  172. *lkcp9.UDPSession,
  173. error,
  174. ) {
  175. block, _ := lkcp9.NewSalsa20BlockCrypt(
  176. pass,
  177. )
  178. sess, err := lkcp9.DialWithOptions(
  179. portTinyBufferEcho,
  180. block,
  181. 10,
  182. 3,
  183. )
  184. if err != nil {
  185. panic(
  186. err,
  187. )
  188. }
  189. runtime.GC()
  190. return sess, err
  191. }
  192. func listenEcho() (
  193. net.Listener,
  194. error,
  195. ) {
  196. block, _ := lkcp9.NewSalsa20BlockCrypt(
  197. pass,
  198. )
  199. runtime.GC()
  200. debug.FreeOSMemory()
  201. return lkcp9.ListenWithOptions(
  202. portEcho,
  203. block,
  204. 10,
  205. 3,
  206. )
  207. }
  208. func listenTinyBufferEcho() (
  209. net.Listener,
  210. error,
  211. ) {
  212. block, _ := lkcp9.NewSalsa20BlockCrypt(
  213. pass,
  214. )
  215. runtime.GC()
  216. debug.FreeOSMemory()
  217. return lkcp9.ListenWithOptions(
  218. portTinyBufferEcho,
  219. block,
  220. 10,
  221. 3,
  222. )
  223. }
  224. func listenSink() (
  225. net.Listener,
  226. error,
  227. ) {
  228. runtime.GC()
  229. debug.FreeOSMemory()
  230. return lkcp9.ListenWithOptions(
  231. portSink,
  232. nil,
  233. 0,
  234. 0,
  235. )
  236. }
  237. func echoServer() {
  238. l, err := listenEcho()
  239. if err != nil {
  240. panic(
  241. err,
  242. )
  243. }
  244. go func() {
  245. Kcplistener := l.(*lkcp9.Listener)
  246. Kcplistener.SetReadBuffer(4 * 1024 * 1024)
  247. Kcplistener.SetWriteBuffer(4 * 1024 * 1024)
  248. Kcplistener.SetDSCP(46)
  249. for {
  250. s, err := l.Accept()
  251. if err != nil {
  252. return
  253. }
  254. s.(*lkcp9.UDPSession).SetReadBuffer(4 * 1024 * 1024)
  255. s.(*lkcp9.UDPSession).SetWriteBuffer(4 * 1024 * 1024)
  256. go handleEcho(s.(*lkcp9.UDPSession))
  257. }
  258. }()
  259. runtime.GC()
  260. debug.FreeOSMemory()
  261. }
  262. func sinkServer() {
  263. l, err := listenSink()
  264. if err != nil {
  265. panic(
  266. err,
  267. )
  268. }
  269. go func() {
  270. Kcplistener := l.(*lkcp9.Listener)
  271. Kcplistener.SetReadBuffer(
  272. 4 * 1024 * 1024,
  273. )
  274. Kcplistener.SetWriteBuffer(
  275. 4 * 1024 * 1024,
  276. )
  277. Kcplistener.SetDSCP(
  278. 46,
  279. )
  280. for {
  281. s, err := l.Accept()
  282. if err != nil {
  283. return
  284. }
  285. go handleSink(s.(*lkcp9.UDPSession))
  286. }
  287. }()
  288. runtime.GC()
  289. debug.FreeOSMemory()
  290. }
  291. func tinyBufferEchoServer() {
  292. l, err := listenTinyBufferEcho()
  293. if err != nil {
  294. panic(
  295. err,
  296. )
  297. }
  298. go func() {
  299. for {
  300. s, err := l.Accept()
  301. if err != nil {
  302. return
  303. }
  304. go handleTinyBufferEcho(s.(*lkcp9.UDPSession))
  305. }
  306. }()
  307. runtime.GC()
  308. debug.FreeOSMemory()
  309. }
  310. func handleEcho(
  311. conn *lkcp9.UDPSession,
  312. ) {
  313. conn.SetStreamMode(
  314. true,
  315. )
  316. conn.SetWindowSize(
  317. 4096,
  318. 4096,
  319. )
  320. conn.SetNoDelay(
  321. 1,
  322. 10,
  323. 2,
  324. 1,
  325. )
  326. conn.SetDSCP(
  327. 46,
  328. )
  329. conn.SetMtu(
  330. 1400,
  331. )
  332. conn.SetACKNoDelay(
  333. false,
  334. )
  335. conn.SetReadDeadline(
  336. time.Now().Add(time.Hour),
  337. )
  338. conn.SetWriteDeadline(
  339. time.Now().Add(time.Hour),
  340. )
  341. buf := make(
  342. []byte,
  343. 65536,
  344. )
  345. for {
  346. n, err := conn.Read(
  347. buf,
  348. )
  349. if err != nil {
  350. panic(
  351. err,
  352. )
  353. }
  354. conn.Write(
  355. buf[:n],
  356. )
  357. }
  358. runtime.GC()
  359. debug.FreeOSMemory()
  360. }
  361. func handleSink(
  362. conn *lkcp9.UDPSession,
  363. ) {
  364. conn.SetStreamMode(
  365. true,
  366. )
  367. conn.SetWindowSize(
  368. 4096,
  369. 4096,
  370. )
  371. conn.SetNoDelay(
  372. 1,
  373. 10,
  374. 2,
  375. 1,
  376. )
  377. conn.SetDSCP(
  378. 46,
  379. )
  380. conn.SetMtu(
  381. 1400,
  382. )
  383. conn.SetACKNoDelay(
  384. false,
  385. )
  386. conn.SetReadDeadline(
  387. time.Now().Add(time.Hour),
  388. )
  389. conn.SetWriteDeadline(
  390. time.Now().Add(time.Hour),
  391. )
  392. buf := make(
  393. []byte,
  394. 65536,
  395. )
  396. for {
  397. _, err := conn.Read(
  398. buf,
  399. )
  400. if err != nil {
  401. panic(
  402. err,
  403. )
  404. }
  405. }
  406. runtime.GC()
  407. debug.FreeOSMemory()
  408. }
  409. func handleTinyBufferEcho(
  410. conn *lkcp9.UDPSession,
  411. ) {
  412. conn.SetStreamMode(
  413. true,
  414. )
  415. buf := make(
  416. []byte,
  417. 2,
  418. )
  419. for {
  420. n, err := conn.Read(
  421. buf,
  422. )
  423. if err != nil {
  424. panic(
  425. err,
  426. )
  427. }
  428. conn.Write(
  429. buf[:n],
  430. )
  431. }
  432. runtime.GC()
  433. debug.FreeOSMemory()
  434. }
  435. func TestTimeout(
  436. t *testing.T,
  437. ) {
  438. defer u.Leakplug(
  439. t,
  440. )
  441. cli, err := dialEcho()
  442. if err != nil {
  443. panic(
  444. err,
  445. )
  446. }
  447. buf := make(
  448. []byte,
  449. 10,
  450. )
  451. cli.SetDeadline(
  452. time.Now().Add(time.Second),
  453. )
  454. <-time.After(
  455. 2 * time.Second,
  456. )
  457. n, err := cli.Read(buf)
  458. if n != 0 || err == nil {
  459. t.Fail()
  460. }
  461. cli.Close()
  462. runtime.GC()
  463. debug.FreeOSMemory()
  464. }
  465. func TestSendRecv(
  466. t *testing.T,
  467. ) {
  468. defer u.Leakplug(
  469. t,
  470. )
  471. cli, err := dialEcho()
  472. if err != nil {
  473. panic(
  474. err,
  475. )
  476. }
  477. cli.SetWriteDelay(
  478. true,
  479. )
  480. cli.SetDUP(
  481. 1,
  482. )
  483. const (
  484. N = 100
  485. )
  486. buf := make(
  487. []byte,
  488. 10,
  489. )
  490. for i := 0; i < N; i++ {
  491. msg := fmt.Sprintf(
  492. "hello%v",
  493. i,
  494. )
  495. cli.Write(
  496. []byte(msg),
  497. )
  498. if n, err := cli.Read(
  499. buf,
  500. ); err == nil {
  501. if string(
  502. buf[:n],
  503. ) != msg {
  504. t.Fail()
  505. }
  506. } else {
  507. panic(
  508. err,
  509. )
  510. }
  511. }
  512. cli.Close()
  513. runtime.GC()
  514. debug.FreeOSMemory()
  515. }
  516. func TestSendVector(
  517. t *testing.T,
  518. ) {
  519. defer u.Leakplug(
  520. t,
  521. )
  522. cli, err := dialEcho()
  523. if err != nil {
  524. panic(
  525. err,
  526. )
  527. }
  528. cli.SetWriteDelay(
  529. false,
  530. )
  531. const N = 100
  532. buf := make(
  533. []byte,
  534. 20,
  535. )
  536. v := make(
  537. [][]byte,
  538. 2,
  539. )
  540. for i := 0; i < N; i++ {
  541. v[0] = []byte(
  542. fmt.Sprintf(
  543. "holas%v",
  544. i,
  545. ))
  546. v[1] = []byte(
  547. fmt.Sprintf(
  548. "amigo%v",
  549. i,
  550. ))
  551. msg :=
  552. fmt.Sprintf(
  553. "holas%vamigo%v",
  554. i,
  555. i,
  556. )
  557. cli.WriteBuffers(
  558. v,
  559. )
  560. if n, err := cli.Read(
  561. buf,
  562. ); err == nil {
  563. if string(
  564. buf[:n],
  565. ) != msg {
  566. t.Error(
  567. string(buf[:n]),
  568. msg,
  569. )
  570. }
  571. } else {
  572. panic(
  573. err,
  574. )
  575. }
  576. }
  577. cli.Close()
  578. runtime.GC()
  579. debug.FreeOSMemory()
  580. }
  581. func TestTinyBufferReceiver(
  582. t *testing.T,
  583. ) {
  584. defer u.Leakplug(
  585. t,
  586. )
  587. cli, err := dialTinyBufferEcho()
  588. if err != nil {
  589. panic(
  590. err,
  591. )
  592. }
  593. const (
  594. N = 100
  595. )
  596. snd := byte(
  597. 0,
  598. )
  599. fillBuffer := func(
  600. buf []byte,
  601. ) {
  602. for i := 0; i < len(
  603. buf,
  604. ); i++ {
  605. buf[i] = snd
  606. snd++
  607. }
  608. }
  609. rcv := byte(
  610. 0,
  611. )
  612. check := func(
  613. buf []byte,
  614. ) bool {
  615. for i := 0; i < len(
  616. buf,
  617. ); i++ {
  618. if buf[i] != rcv {
  619. return false
  620. }
  621. rcv++
  622. }
  623. return true
  624. }
  625. sndbuf := make(
  626. []byte,
  627. 7,
  628. )
  629. rcvbuf := make(
  630. []byte,
  631. 7,
  632. )
  633. for i := 0; i < N; i++ {
  634. fillBuffer(
  635. sndbuf,
  636. )
  637. cli.Write(
  638. sndbuf,
  639. )
  640. if n, err := io.ReadFull(
  641. cli,
  642. rcvbuf,
  643. ); err == nil {
  644. if !check(
  645. rcvbuf[:n],
  646. ) {
  647. t.Fail()
  648. }
  649. } else {
  650. panic(
  651. err,
  652. )
  653. }
  654. }
  655. cli.Close()
  656. runtime.GC()
  657. debug.FreeOSMemory()
  658. }
  659. func TestClose(
  660. t *testing.T,
  661. ) {
  662. defer u.Leakplug(
  663. t,
  664. )
  665. cli, err := dialEcho()
  666. if err != nil {
  667. panic(
  668. err,
  669. )
  670. }
  671. buf := make(
  672. []byte,
  673. 10,
  674. )
  675. cli.Close()
  676. if cli.Close() == nil {
  677. t.Fail()
  678. }
  679. n, err := cli.Write(
  680. buf,
  681. )
  682. if n != 0 || err == nil {
  683. t.Fail()
  684. }
  685. n, err = cli.Read(
  686. buf,
  687. )
  688. if n != 0 || err == nil {
  689. t.Fail()
  690. }
  691. cli.Close()
  692. runtime.GC()
  693. debug.FreeOSMemory()
  694. }
  695. func TestMassivelyParallel_Concurrent_2048_Clients_128_byte_Messages_128_Iterations(
  696. t *testing.T,
  697. ) {
  698. runtime.GC()
  699. debug.FreeOSMemory()
  700. t.Parallel()
  701. _ = runtime.GOMAXPROCS(runtime.NumCPU() * 16)
  702. t.Log(fmt.Sprintf("Starting Goroutines=%v", runtime.NumGoroutine()))
  703. defer u.Leakplug(
  704. t,
  705. )
  706. var wg sync.WaitGroup
  707. wg.Add(
  708. 2048,
  709. )
  710. for i := 0; i < 2048; i++ {
  711. go parallel_client(
  712. &wg,
  713. )
  714. }
  715. t.Log(fmt.Sprintf("Activate Goroutines=%v", runtime.NumGoroutine()))
  716. wg.Wait()
  717. t.Log(fmt.Sprintf("Utilized Goroutines=%v", runtime.NumGoroutine()))
  718. runtime.GC()
  719. debug.FreeOSMemory()
  720. }
  721. func parallel_client(
  722. wg *sync.WaitGroup,
  723. ) (
  724. err error,
  725. ) {
  726. cli, err := dialEcho()
  727. if err != nil {
  728. panic(
  729. err,
  730. )
  731. }
  732. err = echo_tester(
  733. cli,
  734. 128,
  735. 128,
  736. )
  737. wg.Done()
  738. runtime.GC()
  739. debug.FreeOSMemory()
  740. return
  741. }
  742. func BenchmarkEchoSpeed4K(
  743. b *testing.B,
  744. ) {
  745. speedclient(
  746. b,
  747. 4096,
  748. )
  749. runtime.GC()
  750. debug.FreeOSMemory()
  751. }
  752. func BenchmarkEchoSpeed64K(
  753. b *testing.B,
  754. ) {
  755. speedclient(
  756. b,
  757. 65536,
  758. )
  759. runtime.GC()
  760. debug.FreeOSMemory()
  761. }
  762. func BenchmarkEchoSpeed512K(
  763. b *testing.B,
  764. ) {
  765. speedclient(b,
  766. 524288,
  767. )
  768. runtime.GC()
  769. debug.FreeOSMemory()
  770. }
  771. func BenchmarkEchoSpeed1M(
  772. b *testing.B,
  773. ) {
  774. speedclient(
  775. b,
  776. 1048576,
  777. )
  778. runtime.GC()
  779. debug.FreeOSMemory()
  780. }
  781. func speedclient(
  782. b *testing.B,
  783. nbytes int,
  784. ) {
  785. b.ReportAllocs()
  786. cli, err := dialEcho()
  787. if err != nil {
  788. panic(
  789. err,
  790. )
  791. }
  792. if err := echo_tester(
  793. cli,
  794. nbytes,
  795. b.N,
  796. ); err != nil {
  797. b.Fail()
  798. }
  799. b.SetBytes(
  800. int64(nbytes),
  801. )
  802. runtime.GC()
  803. debug.FreeOSMemory()
  804. }
  805. func BenchmarkSinkSpeed4K(
  806. b *testing.B,
  807. ) {
  808. sinkclient(
  809. b,
  810. 4096,
  811. )
  812. runtime.GC()
  813. debug.FreeOSMemory()
  814. }
  815. func BenchmarkSinkSpeed64K(
  816. b *testing.B,
  817. ) {
  818. sinkclient(
  819. b,
  820. 65536,
  821. )
  822. runtime.GC()
  823. debug.FreeOSMemory()
  824. }
  825. func BenchmarkSinkSpeed256K(
  826. b *testing.B,
  827. ) {
  828. sinkclient(
  829. b,
  830. 524288,
  831. )
  832. runtime.GC()
  833. debug.FreeOSMemory()
  834. }
  835. func BenchmarkSinkSpeed1M(
  836. b *testing.B,
  837. ) {
  838. sinkclient(
  839. b,
  840. 1048576,
  841. )
  842. runtime.GC()
  843. debug.FreeOSMemory()
  844. }
  845. func sinkclient(
  846. b *testing.B,
  847. nbytes int,
  848. ) {
  849. b.ReportAllocs()
  850. cli, err := dialSink()
  851. if err != nil {
  852. panic(
  853. err,
  854. )
  855. }
  856. sink_tester(
  857. cli,
  858. nbytes,
  859. b.N,
  860. )
  861. b.SetBytes(
  862. int64(nbytes),
  863. )
  864. runtime.GC()
  865. debug.FreeOSMemory()
  866. }
  867. func echo_tester(
  868. cli net.Conn,
  869. msglen,
  870. msgcount int,
  871. ) error {
  872. buf := make(
  873. []byte,
  874. msglen,
  875. )
  876. for i := 0; i < msgcount; i++ {
  877. if _, err := cli.Write(
  878. buf,
  879. ); err != nil {
  880. return err
  881. }
  882. nrecv := 0
  883. for {
  884. n, err := cli.Read(
  885. buf,
  886. )
  887. if err != nil {
  888. return err
  889. } else {
  890. nrecv += n
  891. if nrecv == msglen {
  892. break
  893. }
  894. }
  895. }
  896. }
  897. runtime.GC()
  898. return nil
  899. }
  900. func sink_tester(
  901. cli *lkcp9.UDPSession,
  902. msglen,
  903. msgcount int,
  904. ) error {
  905. buf := make(
  906. []byte,
  907. msglen,
  908. )
  909. for i := 0; i < msgcount; i++ {
  910. if _, err := cli.Write(
  911. buf,
  912. ); err != nil {
  913. return err
  914. }
  915. }
  916. runtime.GC()
  917. debug.FreeOSMemory()
  918. return nil
  919. }
  920. func TestSNSI(
  921. t *testing.T,
  922. ) {
  923. defer u.Leakplug(
  924. t,
  925. )
  926. t.Log(
  927. *lkcp9.DefaultSnsi.Copy(),
  928. )
  929. t.Log(
  930. lkcp9.DefaultSnsi.Header(),
  931. )
  932. t.Log(
  933. lkcp9.DefaultSnsi.ToSlice(),
  934. )
  935. lkcp9.DefaultSnsi.Reset()
  936. t.Log(
  937. lkcp9.DefaultSnsi.ToSlice(),
  938. )
  939. runtime.GC()
  940. debug.FreeOSMemory()
  941. }
  942. func TestListenerClose(
  943. t *testing.T,
  944. ) {
  945. defer u.Leakplug(
  946. t,
  947. )
  948. l, err := lkcp9.ListenWithOptions(
  949. portListerner,
  950. nil,
  951. 10,
  952. 3,
  953. )
  954. if err != nil {
  955. runtime.GC()
  956. debug.FreeOSMemory()
  957. t.Fail()
  958. }
  959. l.SetReadDeadline(
  960. time.Now().Add(
  961. time.Second,
  962. ))
  963. l.SetWriteDeadline(
  964. time.Now().Add(
  965. time.Second,
  966. ))
  967. l.SetDeadline(
  968. time.Now().Add(
  969. time.Second,
  970. ))
  971. time.Sleep(
  972. 2 * time.Second,
  973. )
  974. if _, err := l.Accept(); err == nil {
  975. t.Fail()
  976. }
  977. runtime.GC()
  978. debug.FreeOSMemory()
  979. l.Close()
  980. fakeaddr, _ := net.ResolveUDPAddr(
  981. "udp6",
  982. "127.0.0.1:1111",
  983. )
  984. if l.CloseSession(
  985. fakeaddr,
  986. ) {
  987. runtime.GC()
  988. debug.FreeOSMemory()
  989. t.Fail()
  990. }
  991. runtime.GC()
  992. debug.FreeOSMemory()
  993. }