CS기초 (Computer Science)
[CS] Reactive Programing 문법 및 개념
커밍이즈스프링
2022. 4. 17. 00:00
반응형
Reactor Cor Feature
- Operations (연산자, 특정 기능을 해주는 함수들)
- Handling errors
- Testing
Operations
- 생성
- create, just, defer, empty, from, ...... - 변환
- flatMap, map, groupBy, buffer, ..... - 필터
- merge, startWith, then, zip, ...... - 오류 처리
- catch, retry - 유틸리티
- delay, do, serialize, subscribe, timestamp, ...... - 조건
- all, contains, defaultIfEmpty, takeUntil, ..... - 집계
- average, count, max, min, reduce, sum, .....
변환 연산자 (flatMap, groupBy, Map)
결합 연산자 (merge, startWith, Then, zip)
오류 처리 연산자 (onErrorResume, retry)
유틸리티 연산자 (delay, do**, serialize, subscribe, timestamp)
조건 연산자 (all, contains, defaultIfEmpty, skipWhile, takeWhile)
집계 연산자 (average* , count, max, min, reduce, sum)
Handling erros
- Publisher가 아이템 처리 시마다 onNext() 이벤트를 발생시키다가, error를 만날 경우 onError() 발생시키고 sequence 중지
- onErrorReturn(), onErrorResume() 등으로 error 발생 시 로직 작성
Testing
- reactor-test dependency 추가
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation 'junit:junit:4.13.1'
- 테스트할 모듈이 flux나 mono를 반환 하도록 하여 검사
- 기대하는 동작을 StepVerifier API로 작성
- StepVerifier를 생성해서 테스트할 시퀀스 입력
- expect*()에 다음에 발생할 기대값 넣음
- 일부 시퀀스를 통과하고 싶을 때는 consumeNextWith(Consumer <T>)
- 종료 이벤트를 기대할 경우 expectComplete(), 혹은 expectError()
- 마지막으로 verify()를 호출하여 검증 단계 trigger 시킴
- 테스트 진행 중 기대한 값과 다른 데이터를 subscribe 하면 즉시 AssertionError 발생하며 테스트 종료 됨
출처: 빗썸 테크아카데미 (코드스테이츠 백엔드 심화과정)
반응형