꿈꾸는 개발자 박상호입니다.
Devhoyas
꿈꾸는 개발자 박상호입니다.
전체 방문자
오늘
어제
  • ALL (17)
    • Algorithm (7)
    • Java (2)
    • Go (2)
    • Spring (3)
    • Database (1)
      • MySQL (1)
      • ElasticSearch (0)
    • Http (1)
    • 일상 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
꿈꾸는 개발자 박상호입니다.

Devhoyas

Go

[Go] context 호출 계층에 따른 결과 테스트하기

2022. 7. 18. 16:16

호출 순서(func) :  main -> a -> ab -> abc
                                                         -> abd
                                               -> ac
                                      -> b
취소 호출(func, time) : main(time.Second * 5)

영향 func : a,b,ab,ac,abc,abd


내용 보강 필요

 

실행 코드

package main

import (
   "context"
   "fmt"
   "sync"
   "time"
)

func main() {

   ctx := context.Background()

   ctx, cancel := context.WithTimeout(ctx, time.Second*5)

   defer cancel()

   var wg sync.WaitGroup

   wg.Add(2)

   go a(ctx, &wg)
   go b(ctx, &wg)

   wg.Wait()

}

func a(ctx context.Context, wg *sync.WaitGroup) {
   tick := time.Tick(time.Second)

   context, cancel := context.WithCancel(ctx)

   defer cancel()

   go ab(context)
   go ac(context)

   for {
      select {
      case <-tick:
         fmt.Println("tick from func A")
      case <-context.Done():
         fmt.Println("done func A")
         wg.Done()
         return
      }
   }
}

func b(ctx context.Context, wg *sync.WaitGroup) {
   tick := time.Tick(time.Second)

   context, cancel := context.WithCancel(ctx)

   defer cancel()

   for {
      select {
      case <-tick:
         fmt.Println("tick from func B")
      case <-context.Done():
         fmt.Println("done func B")
         wg.Done()
         return
      }
   }
}

func ab(ctx context.Context) {
   tick := time.Tick(time.Second)

   context, cancel := context.WithCancel(ctx)

   defer cancel()

   go abc(context)
   go abd(context)

   for {
      select {
      case <-tick:
         fmt.Println("tick from func AB")
      case <-context.Done():
         fmt.Println("done func AB")
         return
      }
   }
}

func ac(ctx context.Context) {
   tick := time.Tick(time.Second)

   for {
      select {
      case <-tick:
         fmt.Println("tick from func AC")
      case <-ctx.Done():
         fmt.Println("done func AC")
         return
      }
   }
}

func abc(ctx context.Context) {
   tick := time.Tick(time.Second)

   context, cancel := context.WithCancel(ctx)

   defer cancel()

   for {
      select {
      case <-tick:
         fmt.Println("tick from func ABC")
      case <-context.Done():
         fmt.Println("done func ABC")
         return
      }
   }
}

func abd(ctx context.Context) {
   tick := time.Tick(time.Second)
   context, cancel := context.WithCancel(ctx)

   defer cancel()

   for {
      select {
      case <-tick:
         fmt.Println("tick from func ABD")
      case <-context.Done():
         fmt.Println("done func ABD")
         return
      }
   }
}
저작자표시 비영리 변경금지

'Go' 카테고리의 다른 글

[Go] Go를이용한 Stack 구현  (0) 2022.07.12
    'Go' 카테고리의 다른 글
    • [Go] Go를이용한 Stack 구현
    꿈꾸는 개발자 박상호입니다.
    꿈꾸는 개발자 박상호입니다.
    취미를 특기로, 특기를 꿈으로, 꿈을 직업으로!

    티스토리툴바