Upgrade to Pro — share decks privately, control downloads, hide ads and more …

net/http/httptest.Server のアプローチをテスト戦略に活用する / Go Conference 2023

net/http/httptest.Server のアプローチをテスト戦略に活用する / Go Conference 2023

Ken’ichiro Oyama

June 02, 2023
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. net/http/httptest.Server ͷΞϓϩʔνΛ
    ςετઓུʹ׆༻͢Δ
    খࢁ݈Ұ࿠ / GMO PEPABO inc.
    2023.6.2 Go Conference 2023 Online
    1

    View full-size slide

  2. ࣗݾ঺հ
    ٕज़෦ɹٕज़ج൫νʔϜ
    2018೥ த్ೖࣾ
    খࢁ ݈Ұ࿠ Ken’ichiro Oyama
    গ࣮͠༻తͰখ͞ͳOSSΛॻ͘ͷ͕झຯɻ
    ● GitHub : k1LoW
    ● Twitter : @k1LoW
    2

    View full-size slide

  3. GMOϖύϘʹ͍ͭͯ
    3
    https://pepabo.com

    View full-size slide

  4. ΞδΣϯμ
    • ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    • goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ
    • goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    • ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ
    • ͓ΘΓʹ
    4

    View full-size slide

  5. 5
    ςεταΠζ෼ྨʹ͓͚Δ
    net/http/httptest.Server ͷϝϦοτ

    View full-size slide

  6. 6
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    ςεταΠζ
    • ςετͷ࣮ߦ଎౓ͱܾఆੑʹண໨ͨ͠ςετ෼ྨ
    • ॻ੶ʮGoogleͷιϑτ΢ΣΞΤϯδχΞϦϯάʯΑΓ
    • Smallςετʢখςετʣ... ୯ҰϓϩηεʢγϯάϧεϨουʣ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ
    ͍ɻsleepېࢭɻI/OېࢭɻϒϩοΫېࢭ
    • Mediumςετʢதςετʣ... ୯ҰϚγϯ಺Ͱ࣮ߦ͞Εͳ͚Ε͹ͳΒͳ͍ɻlocalhostҎ֎ͷγε
    ςϜ΁ͷωοτϫʔΫݺͼग़͠ېࢭ
    • Largeςετʢେςετʣ... ෳ਺Ϛγϯʹ·͕ͨΔςετ

    View full-size slide

  7. 7
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    net/http/httptest.Server
    • httptest.NewServerͰϩʔΧϧϧʔϓόοΫΞυϨε+γεςϜ͕બΜͩϙʔτͰ଴ͪड͚Δ
    HTTPαʔόΛىಈͰ͖Δ
    • HTTPαʔό͸goroutineͰ࣮ߦ͞Ε͍ͯΔ

    View full-size slide

  8. 8
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    HTTPΫϥΠΞϯτ-αʔόؒͷςετΛྫʹͨ͠ൺֱ
    ʢDockerͳͲʣผϓϩηεͰHTTPαʔόΛىಈͯ͠ςετ͢Δ৔߹ͱnet/http/httptest.ServerΛ࢖༻
    ͯ͠ςετ͢Δ৔߹Λൺֱ͢Δ
    • Mediumςετ
    • ߏ੒ཁૉͷ؅ཧ͸Go Runtime
    • ؅ཧΛGo Runtimeʹ೚ͤΔ͜ͱ͕Ͱ͖Δ
    • Mediumςετ
    • ߏ੒ཁૉͷ؅ཧ͸ϝΠϯϓϩηε
    • ಠࣗʹ؅ཧΛ͢Δඞཁ͕͋Δ

    View full-size slide

  9. 9
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test Knowledge from Savanna 202305 edition”
    https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34

    View full-size slide

  10. 10
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    Takuto Wada. “αόϯφศΓʙࣗಈςετʹؔ͢Δ࿈ࡌͰಘΒΕͨ஌ݟͷ·ͱΊʢ2023೥5݄൛ʣʙ / Automated Test Knowledge from Savanna 202305 edition”
    https://speakerdeck.com/twada/automated-test-knowledge-from-savanna-202305-edition?slide=34
    goroutineϕʔεͷMediumςετ

    View full-size slide

  11. 11
    ςεταΠζ෼ྨʹ͓͚Δ net/http/httptest.Server ͷϝϦοτ
    goroutineϕʔεͷMediumςετͷϝϦοτ
    • net/http/httptest.Server͸goroutineϕʔεͷMediumςετΛ࣮ݱͰ͖Δ
    • ҰൠతͳMediumςετͱൺ΂ͯߏ੒ཁૉͷ؅ཧΛGo Runtime (Scheduler) ʹ೚ͤΔ͜ͱ͕Ͱ
    ͖ΔͷͰʢܰྔͰʣ҆ఆͨ͠ӡ༻͕ՄೳʹͳΔ
    • ʮίεύ͕ྑ͍ʯMediumςετͱݴ͑Δ
    • ผϓϩηεͰ࣮ݱ͢ΔMediumςετͱൺ΂ͯɺಉҰͷGo Runtime্Ͱ࣮ߦͰ͖ΔͨΊ஋ͷ૬
    ޓड͚౉͠ͳͲͷ࿈ܞ͕༰қʹ࣮ݱͰ͖Δ
    • ςετμϒϧʢϞοΫɺελϒɺεύΠͳͲʣͷ࣮૷͕͠΍͍͢

    View full-size slide

  12. goroutineϕʔεͷMediumςετΛ
    ޿͘׆༻͢Δςετઓུ
    12

    View full-size slide

  13. • ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍
    • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ
    • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ
    ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ
    • ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦
    APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘
    • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν
    13
    goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ
    ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ

    View full-size slide

  14. 14
    goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ
    ςετର৅ͷΞϓϦέʔγϣϯͱςεταΠζͷΠϝʔδ
    • ઐ༻ͷDBΛ࣋ͪଞͷίϯϙʔωϯτͱ࿈
    ಈ͠ͳ͕Βಈ͘APIαʔόΛྫʹͱΔ
    • Smallςετ͸ςετର৅͕APIαʔό಺
    ෦ɻඞཁʹԠͯ͡ςετμϒϧΛ༻ҙɻ
    • Mediumςετ͸APIαʔόઐ༻DB·Ͱ
    Λςετσʔλϕʔεͱͯ͠༻ҙɻͦͷ
    ଞ͸ςετμϒϧΛ༻ҙɻ
    • Largeςετ͸ίϯϙʔωϯτશମͷಈ
    ࡞͕ର৅

    View full-size slide

  15. 15
    goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ
    ΞʔΩςΫνϟະ׬ঢ়ଶͷΞϓϦέʔγϣϯ
    • ։ൃॳظʹ͓͍ͯSmallςετ͸·ͩݻఆతͰ͸ͳ͍

    View full-size slide

  16. • ࣗಈςετΛؚΉςετεΠʔτͷ༻ҙ͸ૣ͚Ε͹ૣ͍΄ͲಘΒΕΔޮՌ͕େ͖͍
    • ͔͠͠ɺ։ൃॳظ͸ΞϓϦέʔγϣϯͷΞʔΩςΫνϟ͸ະ׬ঢ়ଶ
    • ΞϓϦέʔγϣϯͷ֎ଆ͔ΒͷςετΛް͘͢Δ͜ͱͰɺΞϓϦέʔγϣϯ಺෦ͷ
    ΞʔΩςΫνϟมߋʹڧ͘͢Δ → Mediumςετ
    • ֎෦ͱͷΠϯλʔϑΣʔεͰ͋ΔAPIεΩʔϚ΍ɺσʔλϕʔεεΩʔϚɺ֎෦
    APIΛத৺ʹςετΛ૊Έཱ͍ͯͯ͘
    • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν
    • ίεύ͕ྑ͍ɻ҆ఆ͍ͯ͠ΔɻςετμϒϧΛ࣮ࢪ͠΍͍͢
    16
    goroutineϕʔεͷMediumςετΛ޿͘׆༻͢Δςετઓུ
    ΞϓϦέʔγϣϯͷΞʔΩςΫνϟະ׬ঢ়ଶͰ༗ޮͳςετઓུΛߟ͑Δ

    View full-size slide

  17. 17
    goroutineϕʔεͷMediumςετͷ
    ࡞੒Λαϙʔτ͢Δ

    View full-size slide

  18. 18
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    • goroutineϕʔεͷMediumςετ͸༗ޮͳΞϓϩʔν
    • ΞϓϦέʔγϣϯͷ֎ଆΛΈΔͱϓϩτίϧ͸HTTPʹݶΒͣɺgRPCɺDBʢΫΤϦʣɺSMTP
    ͳͲ͕ߟ͑ΒΕΔ → Mediumςετͷద༻ൣғ͸޿͍
    • net/http/httptestύοέʔδҎ֎ʹ΋ύοέʔδΛ༻ҙͯ͠͞ΒʹޮՌΛ্͛Δ

    View full-size slide

  19. 19
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/httpstub
    • ೚ҙͷϨεϙϯεΛฦ͢HTTPαʔόΛ؆୯ʹ૊Έཱͯͯىಈ͢Δ͜ͱ͕Ͱ͖Δ
    • ͞·͟·ͳ৚݅΍ϨεϙϯεΛઃఆͰ͖ΔͷͰ೚ҙͷHTTPαʔόΛߏஙͰ͖Δ
    • OpenAPI Spec v3ͷDocumentΛಡΈࠐΜͰϦΫΤετͱϨεϙϯεͷόϦσʔγϣϯ͕Ͱ͖Δ
    • OpenAPI Spec v3ͷDocumentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢͜ͱ͕Ͱ
    ͖Δ

    View full-size slide

  20. • ೚ҙͷϨεϙϯεΛฦ͢HTTPαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ
    20
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/httpstub

    View full-size slide

  21. • OpenAPI Spec v3 Documentͷ examples: ηΫγϣϯͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢
    • GET /api/v1/users ͸ examples: ηΫγϣϯͷ 2xx ͳεςʔλεͷ஋Λ࢖ͬͯϨεϙϯεΛฦ͢
    21
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/httpstub

    View full-size slide

  22. 22
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/grpcstub
    • ೚ҙͷϨεϙϯεΛฦ͢gRPCαʔόΛ؆୯ʹ૊Έཱͯͯىಈ͢Δ͜ͱ͕Ͱ͖Δ
    • ͞·͟·ͳ৚݅΍ϨεϙϯεΛઃఆͰ͖ΔͷͰ΄΅೚ҙͷgRPCαʔόΛߏஙͰ͖Δ
    • protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ϦΫΤετͱϨεϙϯεͷόϦσʔγϣϯ͕Ͱ͖Δ
    • protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢͜ͱ͕Ͱ͖Δ

    View full-size slide

  23. 23
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/grpcstub
    • ೚ҙͷϨεϙϯεΛฦ͢gRPCαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ

    View full-size slide

  24. • protoϑΝΠϧͷఆٛΛ࢖༻ͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢
    • routeguide.RouteGuide/GetFeature ͚ͩಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢
    24
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/grpcstub

    View full-size slide

  25. • ೚ҙͷϑΟʔϧυ໊͚ͩಠࣗʹੜ੒ؔ਺Λઃఆͯ͠ಈతʹϨεϙϯεΛ૊Έཱͯͯฦ͢
    25
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/grpcstub

    View full-size slide

  26. 26
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/smtptest
    • net/http/httptest.Serverͱಉ͡Α͏ͳ࢖͍উखͰSMTPαʔόΛىಈ͢Δ͜ͱ͕Ͱ͖Δ
    • ड৴ͨ͠ϝʔϧΛ֬ೝ͢Δ͜ͱ͕Ͱ͖Δ

    View full-size slide

  27. 27
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/smtptest
    • SMTPαʔόΛىಈͯ͠ςετʹ࢖༻͢Δ

    View full-size slide

  28. 28
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/smtptest
    • ड৴ͨ͠ϝʔϧΛ֬ೝ͢Δ͜ͱ͕Ͱ͖Δ

    View full-size slide

  29. 29
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    github.com/k1LoW/runn
    • γφϦΦΛYAMLͰॻ͍ͯͦΕΛ΋ͱʹૢ࡞ʢΦϖϨʔγϣϯʣΛࣗಈԽͰ͖Δ
    • HTTPɺgRPCɺDatabaseʢSQLʣɺChrome DevTools Protocolɺ೚ҙίϚϯυͷ࣮ߦʢϩʔΧ
    ϧ/SSHʣͳͲʹରԠ

    View full-size slide

  30. • runn͸1ͭҎ্ͷγφϦΦΛ࣮ߦͯ͠ɺͦΕͧΕͷ੒ޭ/ࣦഊΛΈΔ
    • 1ͭͷγφϦΦ͸1ͭҎ্ͷεςοϓ͔Βߏ੒͞Ε͍ͯΔ
    • 1γφϦΦ1YAMLϑΝΠϧʹͳ͍ͬͯΔʢϥϯϒοΫͱݺΜͰ͍Δʣ
    • γφϦΦ͝ͱʹϥϯφʔͱݺΜͰ͍Δεςοϓ࣮ߦίϯϙʔωϯτΛఆٛ͠ɺͦͷϥϯφʔΛ࢖֤ͬͯε
    ςοϓΛ࣮ߦ͍ͯ͘͠
    • ϥϯφʔʹ͸γφϦΦ಺ʹఆ͕ٛඞཁͳ௨ৗͷϥϯφʔͱɺطʹrunnͰ༻ҙ͍ͯ͠ΔϏϧτΠϯϥϯφʔͷ2
    छྨ͕͋Δ
    • ϥϯφʔ: HTTPϥϯφʔɺDBϥϯφʔɺgRPCϥϯφʔɺCDPϥϯφʔɺSSHϥϯφʔ
    • ϏϧτΠϯϥϯφʔ: ExecϥϯφʔɺTestϥϯφʔɺDumpϥϯφʔɺIncludeϥϯφʔɺBindϥϯφʔ
    30
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    runnͷΞʔΩςΫνϟ

    View full-size slide

  31. 31
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ
    ϥϯφʔͷઃఆ
    ม਺ͷઃఆ
    ɹɹɹɹ֤εςοϓ
    0
    1
    2
    3

    View full-size slide

  32. 32
    More about runn
    • runnʹ͍ͭͯ͸ɺൃදεϥΠυ΍ZennϒοΫ͕͋ΔͷͰੋඇ͝ཡ͍ͩ͘͞
    • https://speakerdeck.com/k1low/stac2022
    • https://zenn.dev/k1low/books/runn-cookbook
    goroutineϕʔεͷMediumςετͷ࡞੒Λαϙʔτ͢Δ

    View full-size slide

  33. 33
    ࣮ફͯ͠ײ͡Δ
    ϝϦοτͱσϝϦοτ

    View full-size slide

  34. • ҰൠతͳMediumςετͱൺ΂Δͱɺಉ͡Goͷίʔυϕʔεʹด͓ͯ͡Γೝ஌ෛՙ͕খ͘͞ײ͡Δ
    • σʔλϕʔεʹ͍ͭͯ͸ผϓϩηεͳͷͰैདྷ௨Γ
    • ʮClientΛϞοΫ͢ΔςετʯΑΓ΋ɺຊདྷͷڍಈʹ͍ۙʮServer-ClientͰ࣮ࡍʹ௨৴͢Δςετʯ
    ͷ΄͏͕௚ײత͔΋ͯ͠ͳ͍
    • 1γφϦΦ͋ͨΓͷΧόϨοδ͕େ͖͘ɺ݁ՌϦϑΝΫλϦϯά΁ͷ଱ੑ͕େ͖͘ͳ͍ͬͯΔ
    • γφϦΦͷ֦ॆ΋YAMLΛॻ͚ͩ͘Ͱྑ͍ɻҰ౓૊ΈࠐΜͰ͠·͑͹ɺ͋ͱ͸YAMLΛॻ͚ͩ͘ʹͳ
    Δ
    • γφϦΦ͕OpenAPI SpecϥΠΫͰ͋Δ͜ͱ͔ΒΤϯδχΞʹͱͬͯಡΈ΍͘͢ɺͦͷ··ΦϯϘʔ
    σΟϯάʹ໾ʹཱͬͨʢAPIར༻ଆ΁ͷઆ໌ͳͲʣɻॻ͖΍͍͢=ಡΈ΍͍͢
    34
    ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ
    ϝϦοτ

    View full-size slide

  35. • Mediumςετ͸Smallςετͱൺ΂Δͱ࣮ߦ͕࣌ؒ௕͍ɻMediumςετ͕༰қʹ֦
    ॆͰ͖ΔΑ͏ʹͳͬͨ݁Ռɺશମͷςετ࣮ߦ͕࣌ؒେ͖͘ͳΓ͕ͪ
    • ςετ΍σʔλϕʔεΛฒྻԽ͢ΔͳͲͯ͠ର߅͍ͯ͠Δ
    • ΞϓϦέʔγϣϯ֎෦ʹؾܰʹελϒαʔόΛཱͯͯςετ͕Ͱ͖ΔΑ͏ʹͳΔ͜
    ͱͰɺΞϓϦέʔγϣϯࣗମͷςετ༰қੑ͕Լ͕Γ͕ͪ
    • UnitςετΛૄ͔ʹͯ͠͸͍͚ͳ͍ͷ͸໌Β͔ͳͷͰɺΞʔΩςΫνϟະ׬ঢ়
    ଶͱ͸͍͑ςετ༰қੑ͸୲อ͍ͯ͘͠
    35
    ࣮ફͯ͠ײ͡ΔϝϦοτͱσϝϦοτ
    σϝϦοτ

    View full-size slide

  36. • ϓϩμΫτͷ৽ن։ൃ࣌ͷΞʔΩςΫνϟͷະ׬ঢ়ଶʹର߅͢Δखஈͱͯ͠ɺgoroutineϕʔεͷ
    MediumςετΛ޿͘׆༻͢Δํ਑Λऔͬͨ
    • ࠓޙɺʮن໛͕େ͖͘ͳΔʯʮΞʔΩςΫνϟ͕҆ఆ͢ΔʯͳͲͷཁҼʹΑΓςετߏ੒͕มΘ
    Δ͜ͱ͸େ͍ʹ͋Γ͏Δ͕ɺগͳ͘ͱ΋ݱ࣌఺ʹ͓͍ͯ͸ѱ͘ͳ͍൑அͩͬͨ
    37
    ͓ΘΓʹ
    ͓ΘΓʹ

    View full-size slide

  37. Thank You!
    Thank You!
    38

    View full-size slide