github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/deadline/deadline_test.go (about)

     1  package deadline
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestDeadline(t *testing.T) {
     9  	nd := New()
    10  
    11  	pd := NewPipe()
    12  
    13  	go func() {
    14  		now := time.Now()
    15  		<-nd.ctx.Done()
    16  		t.Log("done", time.Since(now))
    17  	}()
    18  
    19  	go func() {
    20  		now := time.Now()
    21  		<-pd.WriteContext().Done()
    22  		t.Log("write done", time.Since(now))
    23  	}()
    24  
    25  	go func() {
    26  		now := time.Now()
    27  		<-pd.ReadContext().Done()
    28  		t.Log("read done", time.Since(now))
    29  	}()
    30  
    31  	pd.SetDeadline(time.Now().Add(2 * time.Second))
    32  	nd.SetDeadline(time.Now().Add(3 * time.Second))
    33  
    34  	<-nd.Context().Done()
    35  	<-pd.WriteContext().Done()
    36  	<-pd.ReadContext().Done()
    37  
    38  	time.Sleep(time.Second)
    39  }